From 59ff3c913449402ad5447bbe3ae6338402fb84b0 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 22 Sep 2010 22:27:05 +0000 Subject: [PATCH] Avoid some Mach-O specific alignment being done on ELF. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@114594 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCAssembler.h | 4 +++- include/llvm/MC/MCObjectStreamer.h | 3 ++- lib/MC/MCAssembler.cpp | 38 ++++++++++++++++-------------- lib/MC/MCELFStreamer.cpp | 2 +- lib/MC/MCMachOStreamer.cpp | 2 +- lib/MC/MCObjectStreamer.cpp | 7 ++++-- lib/MC/WinCOFFStreamer.cpp | 2 +- test/MC/ELF/align.s | 32 +++++++++++++++++++++++++ 8 files changed, 65 insertions(+), 25 deletions(-) create mode 100644 test/MC/ELF/align.s diff --git a/include/llvm/MC/MCAssembler.h b/include/llvm/MC/MCAssembler.h index d193b986a93..0def34c73bb 100644 --- a/include/llvm/MC/MCAssembler.h +++ b/include/llvm/MC/MCAssembler.h @@ -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; } diff --git a/include/llvm/MC/MCObjectStreamer.h b/include/llvm/MC/MCObjectStreamer.h index ea6d9c12338..6836ec52e11 100644 --- a/include/llvm/MC/MCObjectStreamer.h +++ b/include/llvm/MC/MCObjectStreamer.h @@ -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 { diff --git a/lib/MC/MCAssembler.cpp b/lib/MC/MCAssembler.cpp index bb1249950cb..c1dd2888089 100644 --- a/lib/MC/MCAssembler.cpp +++ b/lib/MC/MCAssembler.cpp @@ -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. diff --git a/lib/MC/MCELFStreamer.cpp b/lib/MC/MCELFStreamer.cpp index 9b46b836cae..5dc5ab08dab 100644 --- a/lib/MC/MCELFStreamer.cpp +++ b/lib/MC/MCELFStreamer.cpp @@ -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() {} diff --git a/lib/MC/MCMachOStreamer.cpp b/lib/MC/MCMachOStreamer.cpp index cd6fd50933f..a5f9c2dee50 100644 --- a/lib/MC/MCMachOStreamer.cpp +++ b/lib/MC/MCMachOStreamer.cpp @@ -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 /// @{ diff --git a/lib/MC/MCObjectStreamer.cpp b/lib/MC/MCObjectStreamer.cpp index 2b2385ef915..8a481e8bb30 100644 --- a/lib/MC/MCObjectStreamer.cpp +++ b/lib/MC/MCObjectStreamer.cpp @@ -17,9 +17,12 @@ 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) { } diff --git a/lib/MC/WinCOFFStreamer.cpp b/lib/MC/WinCOFFStreamer.cpp index faecfcbe333..fd1956ec26c 100644 --- a/lib/MC/WinCOFFStreamer.cpp +++ b/lib/MC/WinCOFFStreamer.cpp @@ -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 index 00000000000..d375c4aa4f8 --- /dev/null +++ b/test/MC/ELF/align.s @@ -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) -- 2.34.1