Nick Lewycky | c1f9694 | 2013-03-09 09:31:44 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===// |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file assembles .s files and emits ELF .o object files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Logan Chien | 59ff070 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCELFStreamer.h" |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
David Peixotto | 7266731 | 2013-11-25 19:11:13 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCAsmBackend.h" |
Chandler Carruth | 8a8cd2b | 2014-01-07 11:48:04 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCAssembler.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | 81a6274 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCContext.h" |
Tim Northover | 5cc3dc8 | 2012-12-07 16:50:23 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCELF.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCELFSymbolFlags.h" |
| 23 | #include "llvm/MC/MCExpr.h" |
| 24 | #include "llvm/MC/MCInst.h" |
Rafael Espindola | e308c0c | 2014-01-23 22:49:25 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCObjectFileInfo.h" |
Rafael Espindola | 81a6274 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCObjectStreamer.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCSection.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCSectionELF.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCValue.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 31 | #include "llvm/Support/Debug.h" |
| 32 | #include "llvm/Support/ELF.h" |
| 33 | #include "llvm/Support/ErrorHandling.h" |
| 34 | #include "llvm/Support/raw_ostream.h" |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
Logan Chien | 59ff070 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 38 | MCELFStreamer::~MCELFStreamer() { |
Rafael Espindola | 81a6274 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Rafael Espindola | 8377085 | 2014-03-05 20:09:15 +0000 | [diff] [blame] | 41 | void MCELFStreamer::InitSections() { |
Rafael Espindola | f667d92 | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 42 | // This emulates the same behavior of GNU as. This makes it easier |
| 43 | // to compare the output as the major sections are in the same order. |
Rafael Espindola | 247f951 | 2014-01-24 02:18:40 +0000 | [diff] [blame] | 44 | SwitchSection(getContext().getObjectFileInfo()->getTextSection()); |
Rafael Espindola | 7b51496 | 2014-02-04 18:34:04 +0000 | [diff] [blame] | 45 | EmitCodeAlignment(4); |
Rafael Espindola | 247f951 | 2014-01-24 02:18:40 +0000 | [diff] [blame] | 46 | |
| 47 | SwitchSection(getContext().getObjectFileInfo()->getDataSection()); |
Rafael Espindola | 7b51496 | 2014-02-04 18:34:04 +0000 | [diff] [blame] | 48 | EmitCodeAlignment(4); |
Rafael Espindola | 247f951 | 2014-01-24 02:18:40 +0000 | [diff] [blame] | 49 | |
| 50 | SwitchSection(getContext().getObjectFileInfo()->getBSSSection()); |
Rafael Espindola | 7b51496 | 2014-02-04 18:34:04 +0000 | [diff] [blame] | 51 | EmitCodeAlignment(4); |
Rafael Espindola | 247f951 | 2014-01-24 02:18:40 +0000 | [diff] [blame] | 52 | |
| 53 | SwitchSection(getContext().getObjectFileInfo()->getTextSection()); |
Rafael Espindola | f667d92 | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 56 | void MCELFStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | 1679580 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 57 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 58 | |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 59 | MCObjectStreamer::EmitLabel(Symbol); |
Rafael Espindola | 75d65b9 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 60 | |
Rafael Espindola | 84d0318 | 2010-11-11 19:04:55 +0000 | [diff] [blame] | 61 | const MCSectionELF &Section = |
| 62 | static_cast<const MCSectionELF&>(Symbol->getSection()); |
Rafael Espindola | e5b7415 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 63 | MCSymbolData &SD = getAssembler().getSymbolData(*Symbol); |
Rafael Espindola | 0e7e34e | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 64 | if (Section.getFlags() & ELF::SHF_TLS) |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 65 | MCELF::SetType(SD, ELF::STT_TLS); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Reed Kotler | aee4d5d1 | 2012-12-16 04:00:45 +0000 | [diff] [blame] | 68 | void MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) { |
| 69 | EmitLabel(Symbol); |
| 70 | } |
| 71 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 72 | void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
David Peixotto | 7266731 | 2013-11-25 19:11:13 +0000 | [diff] [blame] | 73 | // Let the target do whatever target specific stuff it needs to do. |
| 74 | getAssembler().getBackend().handleAssemblerFlag(Flag); |
| 75 | // Do any generic stuff we need to do. |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 76 | switch (Flag) { |
Jim Grosbach | 5a2c68d | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 77 | case MCAF_SyntaxUnified: return; // no-op here. |
Evan Cheng | 481ebb0 | 2011-07-27 00:38:12 +0000 | [diff] [blame] | 78 | case MCAF_Code16: return; // Change parsing mode; no-op here. |
| 79 | case MCAF_Code32: return; // Change parsing mode; no-op here. |
| 80 | case MCAF_Code64: return; // Change parsing mode; no-op here. |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 81 | case MCAF_SubsectionsViaSymbols: |
| 82 | getAssembler().setSubsectionsViaSymbols(true); |
| 83 | return; |
| 84 | } |
| 85 | |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 86 | llvm_unreachable("invalid assembler flag!"); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 89 | void MCELFStreamer::ChangeSection(const MCSection *Section, |
| 90 | const MCExpr *Subsection) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 91 | MCSectionData *CurSection = getCurrentSectionData(); |
| 92 | if (CurSection && CurSection->isBundleLocked()) |
| 93 | report_fatal_error("Unterminated .bundle_lock when changing a section"); |
Rafael Espindola | 7d0ba34 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 94 | const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); |
| 95 | if (Grp) |
| 96 | getAssembler().getOrCreateSymbolData(*Grp); |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 97 | this->MCObjectStreamer::ChangeSection(Section, Subsection); |
Rafael Espindola | 7d0ba34 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 100 | void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { |
| 101 | getAssembler().getOrCreateSymbolData(*Symbol); |
Rafael Espindola | 7fadc0e | 2014-03-20 02:12:01 +0000 | [diff] [blame] | 102 | const MCExpr *Value = MCSymbolRefExpr::Create( |
| 103 | Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext()); |
Rafael Espindola | 1614597 | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 104 | Alias->setVariableValue(Value); |
| 105 | } |
| 106 | |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 107 | // When GNU as encounters more than one .type declaration for an object it seems |
| 108 | // to use a mechanism similar to the one below to decide which type is actually |
| 109 | // used in the object file. The greater of T1 and T2 is selected based on the |
| 110 | // following ordering: |
| 111 | // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else |
| 112 | // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user |
| 113 | // provided type). |
| 114 | static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) { |
| 115 | unsigned TypeOrdering[] = {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC, |
| 116 | ELF::STT_GNU_IFUNC, ELF::STT_TLS}; |
| 117 | for (unsigned i = 0; i != array_lengthof(TypeOrdering); ++i) { |
| 118 | if (T1 == TypeOrdering[i]) |
| 119 | return T2; |
| 120 | if (T2 == TypeOrdering[i]) |
| 121 | return T1; |
| 122 | } |
| 123 | |
| 124 | return T2; |
| 125 | } |
| 126 | |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 127 | bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 128 | MCSymbolAttr Attribute) { |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 129 | // Indirect symbols are handled differently, to match how 'as' handles |
| 130 | // them. This makes writing matching .o files easier. |
| 131 | if (Attribute == MCSA_IndirectSymbol) { |
| 132 | // Note that we intentionally cannot use the symbol data here; this is |
| 133 | // important for matching the string table that 'as' generates. |
| 134 | IndirectSymbolData ISD; |
| 135 | ISD.Symbol = Symbol; |
| 136 | ISD.SectionData = getCurrentSectionData(); |
| 137 | getAssembler().getIndirectSymbols().push_back(ISD); |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 138 | return true; |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | // Adding a symbol attribute always introduces the symbol, note that an |
| 142 | // important side effect of calling getOrCreateSymbolData here is to register |
| 143 | // the symbol with the assembler. |
| 144 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 145 | |
| 146 | // The implementation of symbol attributes is designed to match 'as', but it |
| 147 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 148 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 149 | // |
| 150 | // In the future it might be worth trying to make these operations more well |
| 151 | // defined. |
| 152 | switch (Attribute) { |
| 153 | case MCSA_LazyReference: |
| 154 | case MCSA_Reference: |
Kevin Enderby | 8be1441 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 155 | case MCSA_SymbolResolver: |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 156 | case MCSA_PrivateExtern: |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 157 | case MCSA_WeakDefinition: |
Eli Friedman | b20b524 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 158 | case MCSA_WeakDefAutoPrivate: |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 159 | case MCSA_Invalid: |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 160 | case MCSA_IndirectSymbol: |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 161 | return false; |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 162 | |
Jim Grosbach | b7b750d | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 163 | case MCSA_NoDeadStrip: |
Rafael Espindola | eba9022 | 2010-11-14 01:34:31 +0000 | [diff] [blame] | 164 | case MCSA_ELF_TypeGnuUniqueObject: |
| 165 | // Ignore for now. |
| 166 | break; |
| 167 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 168 | case MCSA_Global: |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 169 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 170 | SD.setExternal(true); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 171 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 172 | break; |
| 173 | |
Benjamin Kramer | 5af02b0 | 2010-09-02 17:18:32 +0000 | [diff] [blame] | 174 | case MCSA_WeakReference: |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 175 | case MCSA_Weak: |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 176 | MCELF::SetBinding(SD, ELF::STB_WEAK); |
Rafael Espindola | 83b2a33 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 177 | SD.setExternal(true); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 178 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 179 | break; |
| 180 | |
| 181 | case MCSA_Local: |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 182 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 183 | SD.setExternal(false); |
| 184 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 185 | break; |
| 186 | |
| 187 | case MCSA_ELF_TypeFunction: |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 188 | MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), |
| 189 | ELF::STT_FUNC)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 190 | break; |
| 191 | |
Roman Divacky | 735cb8b | 2011-12-12 17:34:04 +0000 | [diff] [blame] | 192 | case MCSA_ELF_TypeIndFunction: |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 193 | MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), |
| 194 | ELF::STT_GNU_IFUNC)); |
Roman Divacky | 735cb8b | 2011-12-12 17:34:04 +0000 | [diff] [blame] | 195 | break; |
| 196 | |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 197 | case MCSA_ELF_TypeObject: |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 198 | MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), |
| 199 | ELF::STT_OBJECT)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case MCSA_ELF_TypeTLS: |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 203 | MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), |
| 204 | ELF::STT_TLS)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 205 | break; |
| 206 | |
| 207 | case MCSA_ELF_TypeCommon: |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 208 | // TODO: Emit these as a common symbol. |
| 209 | MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), |
| 210 | ELF::STT_OBJECT)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | case MCSA_ELF_TypeNoType: |
Peter Collingbourne | adac407 | 2013-04-10 16:52:15 +0000 | [diff] [blame] | 214 | MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), |
| 215 | ELF::STT_NOTYPE)); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | case MCSA_Protected: |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 219 | MCELF::SetVisibility(SD, ELF::STV_PROTECTED); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 220 | break; |
| 221 | |
| 222 | case MCSA_Hidden: |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 223 | MCELF::SetVisibility(SD, ELF::STV_HIDDEN); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 224 | break; |
| 225 | |
| 226 | case MCSA_Internal: |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 227 | MCELF::SetVisibility(SD, ELF::STV_INTERNAL); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 228 | break; |
| 229 | } |
Saleem Abdulrasool | 4208b61 | 2013-08-09 01:52:03 +0000 | [diff] [blame] | 230 | |
| 231 | return true; |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 235 | unsigned ByteAlignment) { |
| 236 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 237 | |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 238 | if (!BindingExplicitlySet.count(Symbol)) { |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 239 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 240 | SD.setExternal(true); |
| 241 | } |
| 242 | |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 243 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Rafael Espindola | a7d0bed | 2010-11-14 21:11:16 +0000 | [diff] [blame] | 244 | |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 245 | if (MCELF::GetBinding(SD) == ELF_STB_Local) { |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 246 | const MCSection *Section = getAssembler().getContext().getELFSection(".bss", |
Jim Grosbach | 0dde349 | 2011-11-15 16:46:22 +0000 | [diff] [blame] | 247 | ELF::SHT_NOBITS, |
| 248 | ELF::SHF_WRITE | |
| 249 | ELF::SHF_ALLOC, |
| 250 | SectionKind::getBSS()); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 251 | |
| 252 | AssignSection(Symbol, Section); |
Rafael Espindola | 9bac6cb | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 253 | |
Rafael Espindola | 53f0bf1 | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 254 | struct LocalCommon L = {&SD, Size, ByteAlignment}; |
| 255 | LocalCommons.push_back(L); |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 256 | } else { |
| 257 | SD.setCommon(Size, ByteAlignment); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Rafael Espindola | f0591c1 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 260 | SD.setSize(MCConstantExpr::Create(Size, getContext())); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Logan Chien | 59ff070 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 263 | void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 264 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 265 | SD.setSize(Value); |
| 266 | } |
| 267 | |
Benjamin Kramer | 6397051 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 268 | void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 269 | unsigned ByteAlignment) { |
Jason W Kim | 4c1386a | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 270 | // FIXME: Should this be caught and done earlier? |
| 271 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 272 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Jason W Kim | 4c1386a | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 273 | SD.setExternal(false); |
| 274 | BindingExplicitlySet.insert(Symbol); |
Benjamin Kramer | 6397051 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 275 | EmitCommonSymbol(Symbol, Size, ByteAlignment); |
Jason W Kim | 4c1386a | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 278 | void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 279 | if (getCurrentSectionData()->isBundleLocked()) |
| 280 | report_fatal_error("Emitting values inside a locked bundle is forbidden"); |
David Meyer | 44ec69e | 2012-02-15 15:09:06 +0000 | [diff] [blame] | 281 | fixSymbolsInTLSFixups(Value); |
Rafael Espindola | 64e1af8 | 2013-07-02 15:49:13 +0000 | [diff] [blame] | 282 | MCObjectStreamer::EmitValueImpl(Value, Size); |
David Meyer | 44ec69e | 2012-02-15 15:09:06 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 285 | void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 286 | int64_t Value, |
| 287 | unsigned ValueSize, |
| 288 | unsigned MaxBytesToEmit) { |
| 289 | if (getCurrentSectionData()->isBundleLocked()) |
| 290 | report_fatal_error("Emitting values inside a locked bundle is forbidden"); |
| 291 | MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, |
| 292 | ValueSize, MaxBytesToEmit); |
| 293 | } |
| 294 | |
Joerg Sonnenberger | fc18473 | 2013-10-29 01:06:17 +0000 | [diff] [blame] | 295 | // Add a symbol for the file name of this module. They start after the |
| 296 | // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol |
| 297 | // with the same name may appear. |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 298 | void MCELFStreamer::EmitFileDirective(StringRef Filename) { |
Joerg Sonnenberger | fc18473 | 2013-10-29 01:06:17 +0000 | [diff] [blame] | 299 | getAssembler().addFileName(Filename); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Rafael Espindola | 5645bad | 2013-10-16 01:05:45 +0000 | [diff] [blame] | 302 | void MCELFStreamer::EmitIdent(StringRef IdentString) { |
| 303 | const MCSection *Comment = getAssembler().getContext().getELFSection( |
| 304 | ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, |
| 305 | SectionKind::getReadOnly(), 1, ""); |
| 306 | PushSection(); |
| 307 | SwitchSection(Comment); |
| 308 | if (!SeenIdent) { |
| 309 | EmitIntValue(0, 1); |
| 310 | SeenIdent = true; |
| 311 | } |
| 312 | EmitBytes(IdentString); |
| 313 | EmitIntValue(0, 1); |
| 314 | PopSection(); |
| 315 | } |
| 316 | |
| 317 | void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { |
Rafael Espindola | 4e70ac7 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 318 | switch (expr->getKind()) { |
Tim Northover | e0e3aef | 2013-01-31 12:12:40 +0000 | [diff] [blame] | 319 | case MCExpr::Target: |
| 320 | cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler()); |
| 321 | break; |
Rafael Espindola | 4e70ac7 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 322 | case MCExpr::Constant: |
| 323 | break; |
| 324 | |
| 325 | case MCExpr::Binary: { |
| 326 | const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); |
| 327 | fixSymbolsInTLSFixups(be->getLHS()); |
| 328 | fixSymbolsInTLSFixups(be->getRHS()); |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | case MCExpr::SymbolRef: { |
| 333 | const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); |
Rafael Espindola | e98d483 | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 334 | switch (symRef.getKind()) { |
| 335 | default: |
Rafael Espindola | 4e70ac7 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 336 | return; |
Joerg Sonnenberger | 07de07e | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 337 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 338 | case MCSymbolRefExpr::VK_INDNTPOFF: |
Rafael Espindola | e98d483 | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 339 | case MCSymbolRefExpr::VK_NTPOFF: |
| 340 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 341 | case MCSymbolRefExpr::VK_TLSGD: |
Joerg Sonnenberger | 07de07e | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 342 | case MCSymbolRefExpr::VK_TLSLD: |
Rafael Espindola | e98d483 | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 343 | case MCSymbolRefExpr::VK_TLSLDM: |
| 344 | case MCSymbolRefExpr::VK_TPOFF: |
| 345 | case MCSymbolRefExpr::VK_DTPOFF: |
Bruno Cardoso Lopes | 82b077e | 2011-10-25 18:13:20 +0000 | [diff] [blame] | 346 | case MCSymbolRefExpr::VK_Mips_TLSGD: |
| 347 | case MCSymbolRefExpr::VK_Mips_GOTTPREL: |
| 348 | case MCSymbolRefExpr::VK_Mips_TPREL_HI: |
| 349 | case MCSymbolRefExpr::VK_Mips_TPREL_LO: |
Ulrich Weigand | f11efe7 | 2013-07-01 23:33:29 +0000 | [diff] [blame] | 350 | case MCSymbolRefExpr::VK_PPC_DTPMOD: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 351 | case MCSymbolRefExpr::VK_PPC_TPREL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 352 | case MCSymbolRefExpr::VK_PPC_TPREL_LO: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 353 | case MCSymbolRefExpr::VK_PPC_TPREL_HI: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 354 | case MCSymbolRefExpr::VK_PPC_TPREL_HA: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 355 | case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER: |
| 356 | case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA: |
| 357 | case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST: |
| 358 | case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA: |
| 359 | case MCSymbolRefExpr::VK_PPC_DTPREL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 360 | case MCSymbolRefExpr::VK_PPC_DTPREL_LO: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 361 | case MCSymbolRefExpr::VK_PPC_DTPREL_HI: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 362 | case MCSymbolRefExpr::VK_PPC_DTPREL_HA: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 363 | case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER: |
| 364 | case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA: |
| 365 | case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST: |
| 366 | case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA: |
| 367 | case MCSymbolRefExpr::VK_PPC_GOT_TPREL: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 368 | case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 369 | case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 370 | case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 371 | case MCSymbolRefExpr::VK_PPC_GOT_DTPREL: |
| 372 | case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO: |
| 373 | case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI: |
| 374 | case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA: |
Bill Schmidt | 441907d | 2013-02-26 16:41:03 +0000 | [diff] [blame] | 375 | case MCSymbolRefExpr::VK_PPC_TLS: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 376 | case MCSymbolRefExpr::VK_PPC_GOT_TLSGD: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 377 | case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 378 | case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 379 | case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA: |
Ulrich Weigand | 52cf8e4 | 2013-07-09 16:41:09 +0000 | [diff] [blame] | 380 | case MCSymbolRefExpr::VK_PPC_TLSGD: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 381 | case MCSymbolRefExpr::VK_PPC_GOT_TLSLD: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 382 | case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO: |
Ulrich Weigand | 876a0d0 | 2013-06-21 14:44:15 +0000 | [diff] [blame] | 383 | case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI: |
Ulrich Weigand | d51c09f | 2013-06-21 14:42:20 +0000 | [diff] [blame] | 384 | case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA: |
Ulrich Weigand | 52cf8e4 | 2013-07-09 16:41:09 +0000 | [diff] [blame] | 385 | case MCSymbolRefExpr::VK_PPC_TLSLD: |
Rafael Espindola | e98d483 | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 386 | break; |
| 387 | } |
Rafael Espindola | 4e70ac7 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 388 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol()); |
Jan Sjödin | 30a52de | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 389 | MCELF::SetType(SD, ELF::STT_TLS); |
Rafael Espindola | 4e70ac7 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | |
| 393 | case MCExpr::Unary: |
| 394 | fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); |
| 395 | break; |
| 396 | } |
| 397 | } |
| 398 | |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 399 | void MCELFStreamer::EmitInstToFragment(const MCInst &Inst, |
| 400 | const MCSubtargetInfo &STI) { |
| 401 | this->MCObjectStreamer::EmitInstToFragment(Inst, STI); |
Eli Bendersky | 4d9ada0 | 2013-01-08 00:22:56 +0000 | [diff] [blame] | 402 | MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment()); |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 403 | |
Rafael Espindola | 3fa6fc1 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 404 | for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) |
| 405 | fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 406 | } |
| 407 | |
David Woodhouse | 6f3c73f | 2014-01-28 23:12:49 +0000 | [diff] [blame] | 408 | void MCELFStreamer::EmitInstToData(const MCInst &Inst, |
| 409 | const MCSubtargetInfo &STI) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 410 | MCAssembler &Assembler = getAssembler(); |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 411 | SmallVector<MCFixup, 4> Fixups; |
| 412 | SmallString<256> Code; |
| 413 | raw_svector_ostream VecOS(Code); |
David Woodhouse | 9784cef | 2014-01-28 23:13:07 +0000 | [diff] [blame] | 414 | Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI); |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 415 | VecOS.flush(); |
| 416 | |
Rafael Espindola | 4e70ac7 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 417 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 418 | fixSymbolsInTLSFixups(Fixups[i].getValue()); |
| 419 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 420 | // There are several possibilities here: |
| 421 | // |
| 422 | // If bundling is disabled, append the encoded instruction to the current data |
| 423 | // fragment (or create a new such fragment if the current fragment is not a |
| 424 | // data fragment). |
| 425 | // |
| 426 | // If bundling is enabled: |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 427 | // - If we're not in a bundle-locked group, emit the instruction into a |
| 428 | // fragment of its own. If there are no fixups registered for the |
| 429 | // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a |
| 430 | // MCDataFragment. |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 431 | // - If we're in a bundle-locked group, append the instruction to the current |
| 432 | // data fragment because we want all the instructions in a group to get into |
| 433 | // the same fragment. Be careful not to do that for the first instruction in |
| 434 | // the group, though. |
| 435 | MCDataFragment *DF; |
| 436 | |
| 437 | if (Assembler.isBundlingEnabled()) { |
| 438 | MCSectionData *SD = getCurrentSectionData(); |
| 439 | if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst()) |
Derek Schuff | 8878bcc9 | 2013-02-15 22:50:52 +0000 | [diff] [blame] | 440 | // If we are bundle-locked, we re-use the current fragment. |
| 441 | // The bundle-locking directive ensures this is a new data fragment. |
| 442 | DF = cast<MCDataFragment>(getCurrentFragment()); |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 443 | else if (!SD->isBundleLocked() && Fixups.size() == 0) { |
| 444 | // Optimize memory usage by emitting the instruction to a |
| 445 | // MCCompactEncodedInstFragment when not in a bundle-locked group and |
| 446 | // there are no fixups registered. |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 447 | MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment(); |
| 448 | insert(CEIF); |
Eli Bendersky | cf6009b | 2013-01-15 23:22:09 +0000 | [diff] [blame] | 449 | CEIF->getContents().append(Code.begin(), Code.end()); |
| 450 | return; |
Derek Schuff | 8878bcc9 | 2013-02-15 22:50:52 +0000 | [diff] [blame] | 451 | } else { |
Peter Collingbourne | 2f495b9 | 2013-04-17 21:18:16 +0000 | [diff] [blame] | 452 | DF = new MCDataFragment(); |
| 453 | insert(DF); |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 454 | if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) { |
| 455 | // If this is a new fragment created for a bundle-locked group, and the |
| 456 | // group was marked as "align_to_end", set a flag in the fragment. |
| 457 | DF->setAlignToBundleEnd(true); |
| 458 | } |
| 459 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 460 | |
| 461 | // We're now emitting an instruction in a bundle group, so this flag has |
| 462 | // to be turned off. |
| 463 | SD->setBundleGroupBeforeFirstInst(false); |
| 464 | } else { |
| 465 | DF = getOrCreateDataFragment(); |
| 466 | } |
| 467 | |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 468 | // Add the fixups and data. |
| 469 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 470 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
Eli Bendersky | a31a894 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 471 | DF->getFixups().push_back(Fixups[i]); |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 472 | } |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 473 | DF->setHasInstructions(true); |
Benjamin Kramer | 1f60124 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 474 | DF->getContents().append(Code.begin(), Code.end()); |
| 475 | } |
| 476 | |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 477 | void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) { |
| 478 | assert(AlignPow2 <= 30 && "Invalid bundle alignment"); |
| 479 | MCAssembler &Assembler = getAssembler(); |
| 480 | if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0) |
| 481 | Assembler.setBundleAlignSize(1 << AlignPow2); |
| 482 | else |
| 483 | report_fatal_error(".bundle_align_mode should be only set once per file"); |
| 484 | } |
| 485 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 486 | void MCELFStreamer::EmitBundleLock(bool AlignToEnd) { |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 487 | MCSectionData *SD = getCurrentSectionData(); |
| 488 | |
| 489 | // Sanity checks |
| 490 | // |
| 491 | if (!getAssembler().isBundlingEnabled()) |
| 492 | report_fatal_error(".bundle_lock forbidden when bundling is disabled"); |
| 493 | else if (SD->isBundleLocked()) |
| 494 | report_fatal_error("Nesting of .bundle_lock is forbidden"); |
| 495 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 496 | SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd : |
| 497 | MCSectionData::BundleLocked); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 498 | SD->setBundleGroupBeforeFirstInst(true); |
| 499 | } |
| 500 | |
| 501 | void MCELFStreamer::EmitBundleUnlock() { |
| 502 | MCSectionData *SD = getCurrentSectionData(); |
| 503 | |
| 504 | // Sanity checks |
| 505 | if (!getAssembler().isBundlingEnabled()) |
| 506 | report_fatal_error(".bundle_unlock forbidden when bundling is disabled"); |
| 507 | else if (!SD->isBundleLocked()) |
| 508 | report_fatal_error(".bundle_unlock without matching lock"); |
| 509 | else if (SD->isBundleGroupBeforeFirstInst()) |
| 510 | report_fatal_error("Empty bundle-locked group is forbidden"); |
| 511 | |
Eli Bendersky | 802b628 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 512 | SD->setBundleLockState(MCSectionData::NotBundleLocked); |
Eli Bendersky | f483ff9 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 515 | void MCELFStreamer::Flush() { |
Rafael Espindola | 53f0bf1 | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 516 | for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), |
| 517 | e = LocalCommons.end(); |
| 518 | i != e; ++i) { |
| 519 | MCSymbolData *SD = i->SD; |
| 520 | uint64_t Size = i->Size; |
| 521 | unsigned ByteAlignment = i->ByteAlignment; |
| 522 | const MCSymbol &Symbol = SD->getSymbol(); |
| 523 | const MCSection &Section = Symbol.getSection(); |
| 524 | |
| 525 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section); |
| 526 | new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); |
| 527 | |
| 528 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
| 529 | SD->setFragment(F); |
| 530 | |
| 531 | // Update the maximum alignment of the section if necessary. |
| 532 | if (ByteAlignment > SectData.getAlignment()) |
| 533 | SectData.setAlignment(ByteAlignment); |
| 534 | } |
| 535 | |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 536 | LocalCommons.clear(); |
| 537 | } |
| 538 | |
| 539 | void MCELFStreamer::FinishImpl() { |
Craig Topper | bb694de | 2014-04-13 04:57:38 +0000 | [diff] [blame] | 540 | EmitFrames(nullptr, true); |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 541 | |
| 542 | Flush(); |
| 543 | |
Rafael Espindola | 0708209 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 544 | this->MCObjectStreamer::FinishImpl(); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 545 | } |
Richard Mitton | 21101b3 | 2013-09-19 23:21:01 +0000 | [diff] [blame] | 546 | |
Rafael Espindola | 24ea09e | 2014-01-26 06:06:37 +0000 | [diff] [blame] | 547 | MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, |
| 548 | raw_ostream &OS, MCCodeEmitter *CE, |
| 549 | bool RelaxAll, bool NoExecStack) { |
| 550 | MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 551 | if (RelaxAll) |
| 552 | S->getAssembler().setRelaxAll(true); |
Rafael Espindola | b3eca9b | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 553 | if (NoExecStack) |
| 554 | S->getAssembler().setNoExecStack(true); |
Matt Fleming | 6c1ad48 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 555 | return S; |
| 556 | } |
Logan Chien | 59ff070 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 557 | |
Tim Northover | 5cc3dc8 | 2012-12-07 16:50:23 +0000 | [diff] [blame] | 558 | void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 559 | llvm_unreachable("Generic ELF doesn't support this directive"); |
| 560 | } |
| 561 | |
Zoran Jovanovic | 28221d8 | 2014-03-20 09:44:49 +0000 | [diff] [blame] | 562 | MCSymbolData &MCELFStreamer::getOrCreateSymbolData(const MCSymbol *Symbol) { |
Jack Carter | 2f8d9d9 | 2013-02-19 21:57:35 +0000 | [diff] [blame] | 563 | return getAssembler().getOrCreateSymbolData(*Symbol); |
| 564 | } |
| 565 | |
Logan Chien | 59ff070 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 566 | void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 567 | llvm_unreachable("ELF doesn't support this directive"); |
| 568 | } |
| 569 | |
| 570 | void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 571 | llvm_unreachable("ELF doesn't support this directive"); |
| 572 | } |
| 573 | |
| 574 | void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { |
| 575 | llvm_unreachable("ELF doesn't support this directive"); |
| 576 | } |
| 577 | |
| 578 | void MCELFStreamer::EmitCOFFSymbolType(int Type) { |
| 579 | llvm_unreachable("ELF doesn't support this directive"); |
| 580 | } |
| 581 | |
| 582 | void MCELFStreamer::EndCOFFSymbolDef() { |
| 583 | llvm_unreachable("ELF doesn't support this directive"); |
| 584 | } |
| 585 | |
| 586 | void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 587 | uint64_t Size, unsigned ByteAlignment) { |
| 588 | llvm_unreachable("ELF doesn't support this directive"); |
| 589 | } |
| 590 | |
| 591 | void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 592 | uint64_t Size, unsigned ByteAlignment) { |
| 593 | llvm_unreachable("ELF doesn't support this directive"); |
| 594 | } |