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