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