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