Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===// |
| 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 | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCELFStreamer.h" |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallPtrSet.h" |
| 16 | #include "llvm/MC/MCAssembler.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCCodeEmitter.h" |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCContext.h" |
Tim Northover | 6eb3e87 | 2012-12-07 16:50:23 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCELF.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCELFSymbolFlags.h" |
| 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCInst.h" |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCObjectStreamer.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSection.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCSectionELF.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCValue.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/ELF.h" |
| 30 | #include "llvm/Support/ErrorHandling.h" |
| 31 | #include "llvm/Support/raw_ostream.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 32 | |
| 33 | using namespace llvm; |
| 34 | |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 35 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 36 | inline void MCELFStreamer::SetSection(StringRef Section, unsigned Type, |
| 37 | unsigned Flags, SectionKind Kind) { |
| 38 | SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); |
| 39 | } |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 40 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 41 | inline void MCELFStreamer::SetSectionData() { |
| 42 | SetSection(".data", |
| 43 | ELF::SHT_PROGBITS, |
| 44 | ELF::SHF_WRITE | ELF::SHF_ALLOC, |
| 45 | SectionKind::getDataRel()); |
| 46 | EmitCodeAlignment(4, 0); |
| 47 | } |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 48 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 49 | inline void MCELFStreamer::SetSectionText() { |
| 50 | SetSection(".text", |
| 51 | ELF::SHT_PROGBITS, |
| 52 | ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, |
| 53 | SectionKind::getText()); |
| 54 | EmitCodeAlignment(4, 0); |
| 55 | } |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 56 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 57 | inline void MCELFStreamer::SetSectionBss() { |
| 58 | SetSection(".bss", |
| 59 | ELF::SHT_NOBITS, |
| 60 | ELF::SHF_WRITE | ELF::SHF_ALLOC, |
| 61 | SectionKind::getBSS()); |
| 62 | EmitCodeAlignment(4, 0); |
| 63 | } |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 64 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 65 | MCELFStreamer::~MCELFStreamer() { |
Rafael Espindola | f340a29 | 2012-01-07 23:18:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 68 | void MCELFStreamer::InitSections() { |
| 69 | // This emulates the same behavior of GNU as. This makes it easier |
| 70 | // to compare the output as the major sections are in the same order. |
| 71 | SetSectionText(); |
| 72 | SetSectionData(); |
| 73 | SetSectionBss(); |
| 74 | SetSectionText(); |
| 75 | } |
| 76 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 77 | void MCELFStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | ba21024 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 78 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 79 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 80 | MCObjectStreamer::EmitLabel(Symbol); |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 81 | |
Rafael Espindola | e1a2587 | 2010-11-11 19:04:55 +0000 | [diff] [blame] | 82 | const MCSectionELF &Section = |
| 83 | static_cast<const MCSectionELF&>(Symbol->getSection()); |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 84 | MCSymbolData &SD = getAssembler().getSymbolData(*Symbol); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 85 | if (Section.getFlags() & ELF::SHF_TLS) |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 86 | MCELF::SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Reed Kotler | 2c3a464 | 2012-12-16 04:00:45 +0000 | [diff] [blame] | 89 | void MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) { |
| 90 | EmitLabel(Symbol); |
| 91 | } |
| 92 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 93 | void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 94 | switch (Flag) { |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 95 | case MCAF_SyntaxUnified: return; // no-op here. |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame] | 96 | case MCAF_Code16: return; // Change parsing mode; no-op here. |
| 97 | case MCAF_Code32: return; // Change parsing mode; no-op here. |
| 98 | case MCAF_Code64: return; // Change parsing mode; no-op here. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 99 | case MCAF_SubsectionsViaSymbols: |
| 100 | getAssembler().setSubsectionsViaSymbols(true); |
| 101 | return; |
| 102 | } |
| 103 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 104 | llvm_unreachable("invalid assembler flag!"); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 107 | void MCELFStreamer::ChangeSection(const MCSection *Section) { |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 108 | MCSectionData *CurSection = getCurrentSectionData(); |
| 109 | if (CurSection && CurSection->isBundleLocked()) |
| 110 | report_fatal_error("Unterminated .bundle_lock when changing a section"); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 111 | const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); |
| 112 | if (Grp) |
| 113 | getAssembler().getOrCreateSymbolData(*Grp); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 114 | this->MCObjectStreamer::ChangeSection(Section); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 117 | void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { |
| 118 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 119 | MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias); |
| 120 | AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 121 | const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext()); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 122 | Alias->setVariableValue(Value); |
| 123 | } |
| 124 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 125 | void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 126 | MCSymbolAttr Attribute) { |
| 127 | // Indirect symbols are handled differently, to match how 'as' handles |
| 128 | // them. This makes writing matching .o files easier. |
| 129 | if (Attribute == MCSA_IndirectSymbol) { |
| 130 | // Note that we intentionally cannot use the symbol data here; this is |
| 131 | // important for matching the string table that 'as' generates. |
| 132 | IndirectSymbolData ISD; |
| 133 | ISD.Symbol = Symbol; |
| 134 | ISD.SectionData = getCurrentSectionData(); |
| 135 | getAssembler().getIndirectSymbols().push_back(ISD); |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Adding a symbol attribute always introduces the symbol, note that an |
| 140 | // important side effect of calling getOrCreateSymbolData here is to register |
| 141 | // the symbol with the assembler. |
| 142 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 143 | |
| 144 | // The implementation of symbol attributes is designed to match 'as', but it |
| 145 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 146 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 147 | // |
| 148 | // In the future it might be worth trying to make these operations more well |
| 149 | // defined. |
| 150 | switch (Attribute) { |
| 151 | case MCSA_LazyReference: |
| 152 | case MCSA_Reference: |
Kevin Enderby | e8e98d7 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 153 | case MCSA_SymbolResolver: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 154 | case MCSA_PrivateExtern: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 155 | case MCSA_WeakDefinition: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 156 | case MCSA_WeakDefAutoPrivate: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 157 | case MCSA_Invalid: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 158 | case MCSA_IndirectSymbol: |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 159 | llvm_unreachable("Invalid symbol attribute for ELF!"); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 160 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 161 | case MCSA_NoDeadStrip: |
Rafael Espindola | 7e528a1 | 2010-11-14 01:34:31 +0000 | [diff] [blame] | 162 | case MCSA_ELF_TypeGnuUniqueObject: |
| 163 | // Ignore for now. |
| 164 | break; |
| 165 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 166 | case MCSA_Global: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 167 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 168 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 169 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 170 | break; |
| 171 | |
Benjamin Kramer | 230c274 | 2010-09-02 17:18:32 +0000 | [diff] [blame] | 172 | case MCSA_WeakReference: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 173 | case MCSA_Weak: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 174 | MCELF::SetBinding(SD, ELF::STB_WEAK); |
Rafael Espindola | 3223f19 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 175 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 176 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 177 | break; |
| 178 | |
| 179 | case MCSA_Local: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 180 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 181 | SD.setExternal(false); |
| 182 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 183 | break; |
| 184 | |
| 185 | case MCSA_ELF_TypeFunction: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 186 | MCELF::SetType(SD, ELF::STT_FUNC); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 187 | break; |
| 188 | |
Roman Divacky | a0c17a4 | 2011-12-12 17:34:04 +0000 | [diff] [blame] | 189 | case MCSA_ELF_TypeIndFunction: |
| 190 | MCELF::SetType(SD, ELF::STT_GNU_IFUNC); |
| 191 | break; |
| 192 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 193 | case MCSA_ELF_TypeObject: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 194 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 195 | break; |
| 196 | |
| 197 | case MCSA_ELF_TypeTLS: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 198 | MCELF::SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 199 | break; |
| 200 | |
| 201 | case MCSA_ELF_TypeCommon: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 202 | MCELF::SetType(SD, ELF::STT_COMMON); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 203 | break; |
| 204 | |
| 205 | case MCSA_ELF_TypeNoType: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 206 | MCELF::SetType(SD, ELF::STT_NOTYPE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 207 | break; |
| 208 | |
| 209 | case MCSA_Protected: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 210 | MCELF::SetVisibility(SD, ELF::STV_PROTECTED); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 211 | break; |
| 212 | |
| 213 | case MCSA_Hidden: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 214 | MCELF::SetVisibility(SD, ELF::STV_HIDDEN); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 215 | break; |
| 216 | |
| 217 | case MCSA_Internal: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 218 | MCELF::SetVisibility(SD, ELF::STV_INTERNAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 219 | break; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 224 | unsigned ByteAlignment) { |
| 225 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 226 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 227 | if (!BindingExplicitlySet.count(Symbol)) { |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 228 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 229 | SD.setExternal(true); |
| 230 | } |
| 231 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 232 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Rafael Espindola | 55d02f3 | 2010-11-14 21:11:16 +0000 | [diff] [blame] | 233 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 234 | if (MCELF::GetBinding(SD) == ELF_STB_Local) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 235 | const MCSection *Section = getAssembler().getContext().getELFSection(".bss", |
Jim Grosbach | 946227d | 2011-11-15 16:46:22 +0000 | [diff] [blame] | 236 | ELF::SHT_NOBITS, |
| 237 | ELF::SHF_WRITE | |
| 238 | ELF::SHF_ALLOC, |
| 239 | SectionKind::getBSS()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 240 | Symbol->setSection(*Section); |
Rafael Espindola | 1963572 | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 241 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 242 | struct LocalCommon L = {&SD, Size, ByteAlignment}; |
| 243 | LocalCommons.push_back(L); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 244 | } else { |
| 245 | SD.setCommon(Size, ByteAlignment); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 248 | SD.setSize(MCConstantExpr::Create(Size, getContext())); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 251 | void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 252 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 253 | SD.setSize(Value); |
| 254 | } |
| 255 | |
Benjamin Kramer | 36a1601 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 256 | void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 257 | unsigned ByteAlignment) { |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 258 | // FIXME: Should this be caught and done earlier? |
| 259 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 260 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 261 | SD.setExternal(false); |
| 262 | BindingExplicitlySet.insert(Symbol); |
Benjamin Kramer | 36a1601 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 263 | EmitCommonSymbol(Symbol, Size, ByteAlignment); |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 264 | } |
| 265 | |
David Meyer | 5f76926 | 2012-02-15 15:09:06 +0000 | [diff] [blame] | 266 | void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
| 267 | unsigned AddrSpace) { |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 268 | if (getCurrentSectionData()->isBundleLocked()) |
| 269 | report_fatal_error("Emitting values inside a locked bundle is forbidden"); |
David Meyer | 5f76926 | 2012-02-15 15:09:06 +0000 | [diff] [blame] | 270 | fixSymbolsInTLSFixups(Value); |
| 271 | MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace); |
| 272 | } |
| 273 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 274 | void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 275 | int64_t Value, |
| 276 | unsigned ValueSize, |
| 277 | unsigned MaxBytesToEmit) { |
| 278 | if (getCurrentSectionData()->isBundleLocked()) |
| 279 | report_fatal_error("Emitting values inside a locked bundle is forbidden"); |
| 280 | MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value, |
| 281 | ValueSize, MaxBytesToEmit); |
| 282 | } |
| 283 | |
David Meyer | 5f76926 | 2012-02-15 15:09:06 +0000 | [diff] [blame] | 284 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 285 | // Add a symbol for the file name of this module. This is the second |
| 286 | // entry in the module's symbol table (the first being the null symbol). |
| 287 | void MCELFStreamer::EmitFileDirective(StringRef Filename) { |
| 288 | MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 289 | Symbol->setSection(*getCurrentSection()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 290 | Symbol->setAbsolute(); |
| 291 | |
| 292 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 293 | |
| 294 | SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default); |
| 295 | } |
| 296 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 297 | void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { |
| 298 | switch (expr->getKind()) { |
| 299 | case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); |
| 300 | case MCExpr::Constant: |
| 301 | break; |
| 302 | |
| 303 | case MCExpr::Binary: { |
| 304 | const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); |
| 305 | fixSymbolsInTLSFixups(be->getLHS()); |
| 306 | fixSymbolsInTLSFixups(be->getRHS()); |
| 307 | break; |
| 308 | } |
| 309 | |
| 310 | case MCExpr::SymbolRef: { |
| 311 | const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 312 | switch (symRef.getKind()) { |
| 313 | default: |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 314 | return; |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 315 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 316 | case MCSymbolRefExpr::VK_INDNTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 317 | case MCSymbolRefExpr::VK_NTPOFF: |
| 318 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 319 | case MCSymbolRefExpr::VK_TLSGD: |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 320 | case MCSymbolRefExpr::VK_TLSLD: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 321 | case MCSymbolRefExpr::VK_TLSLDM: |
| 322 | case MCSymbolRefExpr::VK_TPOFF: |
| 323 | case MCSymbolRefExpr::VK_DTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 324 | case MCSymbolRefExpr::VK_ARM_TLSGD: |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 325 | case MCSymbolRefExpr::VK_ARM_TPOFF: |
| 326 | case MCSymbolRefExpr::VK_ARM_GOTTPOFF: |
Bruno Cardoso Lopes | 3507d24 | 2011-10-25 18:13:20 +0000 | [diff] [blame] | 327 | case MCSymbolRefExpr::VK_Mips_TLSGD: |
| 328 | case MCSymbolRefExpr::VK_Mips_GOTTPREL: |
| 329 | case MCSymbolRefExpr::VK_Mips_TPREL_HI: |
| 330 | case MCSymbolRefExpr::VK_Mips_TPREL_LO: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 333 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol()); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 334 | MCELF::SetType(SD, ELF::STT_TLS); |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 335 | break; |
| 336 | } |
| 337 | |
| 338 | case MCExpr::Unary: |
| 339 | fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 344 | void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 345 | this->MCObjectStreamer::EmitInstToFragment(Inst); |
Eli Bendersky | 251040b | 2013-01-08 00:22:56 +0000 | [diff] [blame^] | 346 | MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 347 | |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 348 | for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) |
| 349 | fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | void MCELFStreamer::EmitInstToData(const MCInst &Inst) { |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 353 | MCAssembler &Assembler = getAssembler(); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 354 | SmallVector<MCFixup, 4> Fixups; |
| 355 | SmallString<256> Code; |
| 356 | raw_svector_ostream VecOS(Code); |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 357 | Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 358 | VecOS.flush(); |
| 359 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 360 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 361 | fixSymbolsInTLSFixups(Fixups[i].getValue()); |
| 362 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 363 | // There are several possibilities here: |
| 364 | // |
| 365 | // If bundling is disabled, append the encoded instruction to the current data |
| 366 | // fragment (or create a new such fragment if the current fragment is not a |
| 367 | // data fragment). |
| 368 | // |
| 369 | // If bundling is enabled: |
| 370 | // - If we're not in a bundle-locked group, emit the instruction into a data |
| 371 | // fragment of its own. |
| 372 | // - If we're in a bundle-locked group, append the instruction to the current |
| 373 | // data fragment because we want all the instructions in a group to get into |
| 374 | // the same fragment. Be careful not to do that for the first instruction in |
| 375 | // the group, though. |
| 376 | MCDataFragment *DF; |
| 377 | |
| 378 | if (Assembler.isBundlingEnabled()) { |
| 379 | MCSectionData *SD = getCurrentSectionData(); |
| 380 | if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst()) |
| 381 | DF = getOrCreateDataFragment(); |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 382 | else { |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 383 | DF = new MCDataFragment(SD); |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 384 | if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) { |
| 385 | // If this is a new fragment created for a bundle-locked group, and the |
| 386 | // group was marked as "align_to_end", set a flag in the fragment. |
| 387 | DF->setAlignToBundleEnd(true); |
| 388 | } |
| 389 | } |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 390 | |
| 391 | // We're now emitting an instruction in a bundle group, so this flag has |
| 392 | // to be turned off. |
| 393 | SD->setBundleGroupBeforeFirstInst(false); |
| 394 | } else { |
| 395 | DF = getOrCreateDataFragment(); |
| 396 | } |
| 397 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 398 | // Add the fixups and data. |
| 399 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 400 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
Eli Bendersky | 64d9a32 | 2012-12-07 19:13:57 +0000 | [diff] [blame] | 401 | DF->getFixups().push_back(Fixups[i]); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 402 | } |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 403 | DF->setHasInstructions(true); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 404 | DF->getContents().append(Code.begin(), Code.end()); |
| 405 | } |
| 406 | |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 407 | void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) { |
| 408 | assert(AlignPow2 <= 30 && "Invalid bundle alignment"); |
| 409 | MCAssembler &Assembler = getAssembler(); |
| 410 | if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0) |
| 411 | Assembler.setBundleAlignSize(1 << AlignPow2); |
| 412 | else |
| 413 | report_fatal_error(".bundle_align_mode should be only set once per file"); |
| 414 | } |
| 415 | |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 416 | void MCELFStreamer::EmitBundleLock(bool AlignToEnd) { |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 417 | MCSectionData *SD = getCurrentSectionData(); |
| 418 | |
| 419 | // Sanity checks |
| 420 | // |
| 421 | if (!getAssembler().isBundlingEnabled()) |
| 422 | report_fatal_error(".bundle_lock forbidden when bundling is disabled"); |
| 423 | else if (SD->isBundleLocked()) |
| 424 | report_fatal_error("Nesting of .bundle_lock is forbidden"); |
| 425 | |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 426 | SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd : |
| 427 | MCSectionData::BundleLocked); |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 428 | SD->setBundleGroupBeforeFirstInst(true); |
| 429 | } |
| 430 | |
| 431 | void MCELFStreamer::EmitBundleUnlock() { |
| 432 | MCSectionData *SD = getCurrentSectionData(); |
| 433 | |
| 434 | // Sanity checks |
| 435 | if (!getAssembler().isBundlingEnabled()) |
| 436 | report_fatal_error(".bundle_unlock forbidden when bundling is disabled"); |
| 437 | else if (!SD->isBundleLocked()) |
| 438 | report_fatal_error(".bundle_unlock without matching lock"); |
| 439 | else if (SD->isBundleGroupBeforeFirstInst()) |
| 440 | report_fatal_error("Empty bundle-locked group is forbidden"); |
| 441 | |
Eli Bendersky | 6c1d497 | 2013-01-07 21:51:08 +0000 | [diff] [blame] | 442 | SD->setBundleLockState(MCSectionData::NotBundleLocked); |
Eli Bendersky | 4766ef4 | 2012-12-20 19:05:53 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 445 | void MCELFStreamer::FinishImpl() { |
Rafael Espindola | c25dad8 | 2011-05-10 03:14:15 +0000 | [diff] [blame] | 446 | EmitFrames(true); |
Rafael Espindola | c70a1d9 | 2010-11-01 17:07:14 +0000 | [diff] [blame] | 447 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 448 | for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), |
| 449 | e = LocalCommons.end(); |
| 450 | i != e; ++i) { |
| 451 | MCSymbolData *SD = i->SD; |
| 452 | uint64_t Size = i->Size; |
| 453 | unsigned ByteAlignment = i->ByteAlignment; |
| 454 | const MCSymbol &Symbol = SD->getSymbol(); |
| 455 | const MCSection &Section = Symbol.getSection(); |
| 456 | |
| 457 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section); |
| 458 | new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); |
| 459 | |
| 460 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
| 461 | SD->setFragment(F); |
| 462 | |
| 463 | // Update the maximum alignment of the section if necessary. |
| 464 | if (ByteAlignment > SectData.getAlignment()) |
| 465 | SectData.setAlignment(ByteAlignment); |
| 466 | } |
| 467 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 468 | this->MCObjectStreamer::FinishImpl(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 469 | } |
Tim Northover | 6eb3e87 | 2012-12-07 16:50:23 +0000 | [diff] [blame] | 470 | void MCELFStreamer::EmitTCEntry(const MCSymbol &S) { |
Adhemerval Zanella | f35c62b | 2012-10-15 15:43:14 +0000 | [diff] [blame] | 471 | // Creates a R_PPC64_TOC relocation |
| 472 | MCObjectStreamer::EmitSymbolValue(&S, 8, 0); |
| 473 | } |
| 474 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 475 | MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 476 | raw_ostream &OS, MCCodeEmitter *CE, |
| 477 | bool RelaxAll, bool NoExecStack) { |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 478 | MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 479 | if (RelaxAll) |
| 480 | S->getAssembler().setRelaxAll(true); |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 481 | if (NoExecStack) |
| 482 | S->getAssembler().setNoExecStack(true); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 483 | return S; |
| 484 | } |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 485 | |
Tim Northover | 6eb3e87 | 2012-12-07 16:50:23 +0000 | [diff] [blame] | 486 | void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 487 | llvm_unreachable("Generic ELF doesn't support this directive"); |
| 488 | } |
| 489 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 490 | void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 491 | llvm_unreachable("ELF doesn't support this directive"); |
| 492 | } |
| 493 | |
| 494 | void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 495 | llvm_unreachable("ELF doesn't support this directive"); |
| 496 | } |
| 497 | |
| 498 | void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { |
| 499 | llvm_unreachable("ELF doesn't support this directive"); |
| 500 | } |
| 501 | |
| 502 | void MCELFStreamer::EmitCOFFSymbolType(int Type) { |
| 503 | llvm_unreachable("ELF doesn't support this directive"); |
| 504 | } |
| 505 | |
| 506 | void MCELFStreamer::EndCOFFSymbolDef() { |
| 507 | llvm_unreachable("ELF doesn't support this directive"); |
| 508 | } |
| 509 | |
| 510 | void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 511 | uint64_t Size, unsigned ByteAlignment) { |
| 512 | llvm_unreachable("ELF doesn't support this directive"); |
| 513 | } |
| 514 | |
| 515 | void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 516 | uint64_t Size, unsigned ByteAlignment) { |
| 517 | llvm_unreachable("ELF doesn't support this directive"); |
| 518 | } |