Avoid some Mach-O specific alignment being done on ELF.
authorRafael Espindola <rafael.espindola@gmail.com>
Wed, 22 Sep 2010 22:27:05 +0000 (22:27 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Wed, 22 Sep 2010 22:27:05 +0000 (22:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@114594 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/MC/MCAssembler.h
include/llvm/MC/MCObjectStreamer.h
lib/MC/MCAssembler.cpp
lib/MC/MCELFStreamer.cpp
lib/MC/MCMachOStreamer.cpp
lib/MC/MCObjectStreamer.cpp
lib/MC/WinCOFFStreamer.cpp
test/MC/ELF/align.s [new file with mode: 0644]

index d193b986a9343bf9b4e11a933283551c15cf5253..0def34c73bb6ef9b73ade7a58b2c03e56c249819 100644 (file)
@@ -605,6 +605,7 @@ private:
 
   unsigned RelaxAll : 1;
   unsigned SubsectionsViaSymbols : 1;
+  unsigned PadSectionToAlignment : 1;
 
 private:
   /// Evaluate a fixup to a relocatable expression and the value which should be
@@ -676,7 +677,8 @@ public:
   // option is to make this abstract, and have targets provide concrete
   // implementations as we do with AsmParser.
   MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
-              MCCodeEmitter &_Emitter, raw_ostream &OS);
+              MCCodeEmitter &_Emitter, bool _PadSectionToAlignment,
+              raw_ostream &OS);
   ~MCAssembler();
 
   MCContext &getContext() const { return Context; }
index ea6d9c12338df405b22a4ad2258fd73d9408e65f..6836ec52e117d0034f71114264baca1209bae288 100644 (file)
@@ -35,7 +35,8 @@ class MCObjectStreamer : public MCStreamer {
 
 protected:
   MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
-                   raw_ostream &_OS, MCCodeEmitter *_Emitter);
+                   raw_ostream &_OS, MCCodeEmitter *_Emitter,
+                   bool _PadSectionToAlignment);
   ~MCObjectStreamer();
 
   MCSectionData *getCurrentSectionData() const {
index bb1249950cba9486f5b49aa866b164fbbbac1131..c1dd288808935fe04df171c0e1cfa46747c72c59 100644 (file)
@@ -221,9 +221,11 @@ MCSymbolData::MCSymbolData(const MCSymbol &_Symbol, MCFragment *_Fragment,
 /* *** */
 
 MCAssembler::MCAssembler(MCContext &_Context, TargetAsmBackend &_Backend,
-                         MCCodeEmitter &_Emitter, raw_ostream &_OS)
+                         MCCodeEmitter &_Emitter, bool _PadSectionToAlignment,
+                         raw_ostream &_OS)
   : Context(_Context), Backend(_Backend), Emitter(_Emitter),
-    OS(_OS), RelaxAll(false), SubsectionsViaSymbols(false)
+    OS(_OS), RelaxAll(false), SubsectionsViaSymbols(false),
+    PadSectionToAlignment(_PadSectionToAlignment)
 {
 }
 
@@ -712,25 +714,25 @@ void MCAssembler::Finish(MCObjectWriter *Writer) {
   // Insert additional align fragments for concrete sections to explicitly pad
   // the previous section to match their alignment requirements. This is for
   // 'gas' compatibility, it shouldn't strictly be necessary.
-  //
-  // FIXME: This may be Mach-O specific.
-  for (unsigned i = 1, e = Layout.getSectionOrder().size(); i < e; ++i) {
-    MCSectionData *SD = Layout.getSectionOrder()[i];
+  if (PadSectionToAlignment) {
+    for (unsigned i = 1, e = Layout.getSectionOrder().size(); i < e; ++i) {
+      MCSectionData *SD = Layout.getSectionOrder()[i];
 
-    // Ignore sections without alignment requirements.
-    unsigned Align = SD->getAlignment();
-    if (Align <= 1)
-      continue;
+      // Ignore sections without alignment requirements.
+      unsigned Align = SD->getAlignment();
+      if (Align <= 1)
+        continue;
 
-    // Ignore virtual sections, they don't cause file size modifications.
-    if (getBackend().isVirtualSection(SD->getSection()))
-      continue;
+      // Ignore virtual sections, they don't cause file size modifications.
+      if (getBackend().isVirtualSection(SD->getSection()))
+        continue;
 
-    // Otherwise, create a new align fragment at the end of the previous
-    // section.
-    MCAlignFragment *AF = new MCAlignFragment(Align, 0, 1, Align,
-                                              Layout.getSectionOrder()[i - 1]);
-    AF->setOnlyAlignAddress(true);
+      // Otherwise, create a new align fragment at the end of the previous
+      // section.
+      MCAlignFragment *AF = new MCAlignFragment(Align, 0, 1, Align,
+                                                Layout.getSectionOrder()[i - 1]);
+      AF->setOnlyAlignAddress(true);
+    }
   }
 
   // Create dummy fragments and assign section ordinals.
index 9b46b836caeca69bfe86b3ecb3e374ef36f08dd8..5dc5ab08dab67d3eb25c74c7a9982a0bea32f6b9 100644 (file)
@@ -40,7 +40,7 @@ class MCELFStreamer : public MCObjectStreamer {
 public:
   MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
                   raw_ostream &OS, MCCodeEmitter *Emitter)
-    : MCObjectStreamer(Context, TAB, OS, Emitter) {}
+    : MCObjectStreamer(Context, TAB, OS, Emitter, false) {}
 
   ~MCELFStreamer() {}
 
index cd6fd50933f0a09b10474d10f1e6fc74233e299c..a5f9c2dee508eab48a9288ff9ff03cfe26b725fa 100644 (file)
@@ -41,7 +41,7 @@ private:
 public:
   MCMachOStreamer(MCContext &Context, TargetAsmBackend &TAB,
                   raw_ostream &OS, MCCodeEmitter *Emitter)
-    : MCObjectStreamer(Context, TAB, OS, Emitter) {}
+    : MCObjectStreamer(Context, TAB, OS, Emitter, true) {}
 
   /// @name MCStreamer Interface
   /// @{
index 2b2385ef9156d8b89fb27f002f3630ec8c3f91e1..8a481e8bb3052eef262cb45d6863488d49538e74 100644 (file)
 using namespace llvm;
 
 MCObjectStreamer::MCObjectStreamer(MCContext &Context, TargetAsmBackend &TAB,
-                                   raw_ostream &_OS, MCCodeEmitter *_Emitter)
+                                   raw_ostream &_OS, MCCodeEmitter *_Emitter,
+                                   bool _PadSectionToAlignment)
   : MCStreamer(Context), Assembler(new MCAssembler(Context, TAB,
-                                                   *_Emitter, _OS)),
+                                                   *_Emitter,
+                                                   _PadSectionToAlignment,
+                                                   _OS)),
     CurSectionData(0)
 {
 }
index faecfcbe333b42affd18b3e32dde75db3a486aae..fd1956ec26c74ac043fa8934b47ef038d830ce09 100644 (file)
@@ -86,7 +86,7 @@ WinCOFFStreamer::WinCOFFStreamer(MCContext &Context,
                                  TargetAsmBackend &TAB,
                                  MCCodeEmitter &CE,
                                  raw_ostream &OS)
-    : MCObjectStreamer(Context, TAB, OS, &CE)
+    : MCObjectStreamer(Context, TAB, OS, &CE, true)
     , CurSymbol(NULL) {
 }
 
diff --git a/test/MC/ELF/align.s b/test/MC/ELF/align.s
new file mode 100644 (file)
index 0000000..d375c4a
--- /dev/null
@@ -0,0 +1,32 @@
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-linux-gnu %s -o - | elf-dump  | FileCheck %s
+
+// Test that the alignment of rodata doesn't force a alignment of the
+// previous section (.bss)
+
+       nop
+       .section        .rodata,"a",@progbits
+       .align  8
+
+// CHECK: # Section 3
+// CHECK-NEXT:  (('sh_name', 13) # '.bss'
+// CHECK-NEXT:   ('sh_type', 8)
+// CHECK-NEXT:   ('sh_flags', 3)
+// CHECK-NEXT:   ('sh_addr', 0)
+// CHECK-NEXT:   ('sh_offset', 68)
+// CHECK-NEXT:   ('sh_size', 0)
+// CHECK-NEXT:   ('sh_link', 0)
+// CHECK-NEXT:   ('sh_info', 0)
+// CHECK-NEXT:   ('sh_addralign', 4)
+// CHECK-NEXT:   ('sh_entsize', 0)
+// CHECK-NEXT:  ),
+// CHECK-NEXT:  # Section 4
+// CHECK-NEXT:  (('sh_name', 18) # '.rodata'
+// CHECK-NEXT:   ('sh_type', 1)
+// CHECK-NEXT:   ('sh_flags', 2)
+// CHECK-NEXT:   ('sh_addr', 0)
+// CHECK-NEXT:   ('sh_offset', 72)
+// CHECK-NEXT:   ('sh_size', 0)
+// CHECK-NEXT:   ('sh_link', 0)
+// CHECK-NEXT:   ('sh_info', 0)
+// CHECK-NEXT:   ('sh_addralign', 8)
+// CHECK-NEXT:   ('sh_entsize', 0)