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 | |
| 89 | void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 90 | switch (Flag) { |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 91 | case MCAF_SyntaxUnified: return; // no-op here. |
Evan Cheng | bd27f5a | 2011-07-27 00:38:12 +0000 | [diff] [blame] | 92 | case MCAF_Code16: return; // Change parsing mode; no-op here. |
| 93 | case MCAF_Code32: return; // Change parsing mode; no-op here. |
| 94 | case MCAF_Code64: return; // Change parsing mode; no-op here. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 95 | case MCAF_SubsectionsViaSymbols: |
| 96 | getAssembler().setSubsectionsViaSymbols(true); |
| 97 | return; |
| 98 | } |
| 99 | |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 100 | llvm_unreachable("invalid assembler flag!"); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 104 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 105 | // MCObjectStreamer. |
| 106 | // FIXME: Lift context changes into super class. |
| 107 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 108 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 109 | } |
| 110 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 111 | void MCELFStreamer::ChangeSection(const MCSection *Section) { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 112 | const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); |
| 113 | if (Grp) |
| 114 | getAssembler().getOrCreateSymbolData(*Grp); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 115 | this->MCObjectStreamer::ChangeSection(Section); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 118 | void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { |
| 119 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 120 | MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias); |
| 121 | AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 122 | const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext()); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 123 | Alias->setVariableValue(Value); |
| 124 | } |
| 125 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 126 | void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 127 | MCSymbolAttr Attribute) { |
| 128 | // Indirect symbols are handled differently, to match how 'as' handles |
| 129 | // them. This makes writing matching .o files easier. |
| 130 | if (Attribute == MCSA_IndirectSymbol) { |
| 131 | // Note that we intentionally cannot use the symbol data here; this is |
| 132 | // important for matching the string table that 'as' generates. |
| 133 | IndirectSymbolData ISD; |
| 134 | ISD.Symbol = Symbol; |
| 135 | ISD.SectionData = getCurrentSectionData(); |
| 136 | getAssembler().getIndirectSymbols().push_back(ISD); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | // Adding a symbol attribute always introduces the symbol, note that an |
| 141 | // important side effect of calling getOrCreateSymbolData here is to register |
| 142 | // the symbol with the assembler. |
| 143 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 144 | |
| 145 | // The implementation of symbol attributes is designed to match 'as', but it |
| 146 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 147 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 148 | // |
| 149 | // In the future it might be worth trying to make these operations more well |
| 150 | // defined. |
| 151 | switch (Attribute) { |
| 152 | case MCSA_LazyReference: |
| 153 | case MCSA_Reference: |
Kevin Enderby | e8e98d7 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 154 | case MCSA_SymbolResolver: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 155 | case MCSA_PrivateExtern: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 156 | case MCSA_WeakDefinition: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 157 | case MCSA_WeakDefAutoPrivate: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 158 | case MCSA_Invalid: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 159 | case MCSA_IndirectSymbol: |
Craig Topper | 8581438 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 160 | llvm_unreachable("Invalid symbol attribute for ELF!"); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 161 | |
Jim Grosbach | 3f90a4c | 2012-09-13 23:11:31 +0000 | [diff] [blame] | 162 | case MCSA_NoDeadStrip: |
Rafael Espindola | 7e528a1 | 2010-11-14 01:34:31 +0000 | [diff] [blame] | 163 | case MCSA_ELF_TypeGnuUniqueObject: |
| 164 | // Ignore for now. |
| 165 | break; |
| 166 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 167 | case MCSA_Global: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 168 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 169 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 170 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 171 | break; |
| 172 | |
Benjamin Kramer | 230c274 | 2010-09-02 17:18:32 +0000 | [diff] [blame] | 173 | case MCSA_WeakReference: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 174 | case MCSA_Weak: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 175 | MCELF::SetBinding(SD, ELF::STB_WEAK); |
Rafael Espindola | 3223f19 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 176 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 177 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 178 | break; |
| 179 | |
| 180 | case MCSA_Local: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 181 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 182 | SD.setExternal(false); |
| 183 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 184 | break; |
| 185 | |
| 186 | case MCSA_ELF_TypeFunction: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 187 | MCELF::SetType(SD, ELF::STT_FUNC); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 188 | break; |
| 189 | |
Roman Divacky | a0c17a4 | 2011-12-12 17:34:04 +0000 | [diff] [blame] | 190 | case MCSA_ELF_TypeIndFunction: |
| 191 | MCELF::SetType(SD, ELF::STT_GNU_IFUNC); |
| 192 | break; |
| 193 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 194 | case MCSA_ELF_TypeObject: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 195 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 196 | break; |
| 197 | |
| 198 | case MCSA_ELF_TypeTLS: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 199 | MCELF::SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 200 | break; |
| 201 | |
| 202 | case MCSA_ELF_TypeCommon: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 203 | MCELF::SetType(SD, ELF::STT_COMMON); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 204 | break; |
| 205 | |
| 206 | case MCSA_ELF_TypeNoType: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 207 | MCELF::SetType(SD, ELF::STT_NOTYPE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 208 | break; |
| 209 | |
| 210 | case MCSA_Protected: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 211 | MCELF::SetVisibility(SD, ELF::STV_PROTECTED); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 212 | break; |
| 213 | |
| 214 | case MCSA_Hidden: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 215 | MCELF::SetVisibility(SD, ELF::STV_HIDDEN); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 216 | break; |
| 217 | |
| 218 | case MCSA_Internal: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 219 | MCELF::SetVisibility(SD, ELF::STV_INTERNAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 225 | unsigned ByteAlignment) { |
| 226 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 227 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 228 | if (!BindingExplicitlySet.count(Symbol)) { |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 229 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 230 | SD.setExternal(true); |
| 231 | } |
| 232 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 233 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Rafael Espindola | 55d02f3 | 2010-11-14 21:11:16 +0000 | [diff] [blame] | 234 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 235 | if (MCELF::GetBinding(SD) == ELF_STB_Local) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 236 | const MCSection *Section = getAssembler().getContext().getELFSection(".bss", |
Jim Grosbach | 946227d | 2011-11-15 16:46:22 +0000 | [diff] [blame] | 237 | ELF::SHT_NOBITS, |
| 238 | ELF::SHF_WRITE | |
| 239 | ELF::SHF_ALLOC, |
| 240 | SectionKind::getBSS()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 241 | Symbol->setSection(*Section); |
Rafael Espindola | 1963572 | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 242 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 243 | struct LocalCommon L = {&SD, Size, ByteAlignment}; |
| 244 | LocalCommons.push_back(L); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 245 | } else { |
| 246 | SD.setCommon(Size, ByteAlignment); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 249 | SD.setSize(MCConstantExpr::Create(Size, getContext())); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 252 | void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 253 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 254 | SD.setSize(Value); |
| 255 | } |
| 256 | |
Benjamin Kramer | 36a1601 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 257 | void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 258 | unsigned ByteAlignment) { |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 259 | // FIXME: Should this be caught and done earlier? |
| 260 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 261 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 262 | SD.setExternal(false); |
| 263 | BindingExplicitlySet.insert(Symbol); |
Benjamin Kramer | 36a1601 | 2011-09-01 23:04:27 +0000 | [diff] [blame] | 264 | EmitCommonSymbol(Symbol, Size, ByteAlignment); |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 265 | } |
| 266 | |
David Meyer | 5f76926 | 2012-02-15 15:09:06 +0000 | [diff] [blame] | 267 | void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size, |
| 268 | unsigned AddrSpace) { |
| 269 | fixSymbolsInTLSFixups(Value); |
| 270 | MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace); |
| 271 | } |
| 272 | |
| 273 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 274 | // Add a symbol for the file name of this module. This is the second |
| 275 | // entry in the module's symbol table (the first being the null symbol). |
| 276 | void MCELFStreamer::EmitFileDirective(StringRef Filename) { |
| 277 | MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 278 | Symbol->setSection(*getCurrentSection()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 279 | Symbol->setAbsolute(); |
| 280 | |
| 281 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 282 | |
| 283 | SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default); |
| 284 | } |
| 285 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 286 | void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { |
| 287 | switch (expr->getKind()) { |
| 288 | case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); |
| 289 | case MCExpr::Constant: |
| 290 | break; |
| 291 | |
| 292 | case MCExpr::Binary: { |
| 293 | const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); |
| 294 | fixSymbolsInTLSFixups(be->getLHS()); |
| 295 | fixSymbolsInTLSFixups(be->getRHS()); |
| 296 | break; |
| 297 | } |
| 298 | |
| 299 | case MCExpr::SymbolRef: { |
| 300 | const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 301 | switch (symRef.getKind()) { |
| 302 | default: |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 303 | return; |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 304 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 305 | case MCSymbolRefExpr::VK_INDNTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 306 | case MCSymbolRefExpr::VK_NTPOFF: |
| 307 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 308 | case MCSymbolRefExpr::VK_TLSGD: |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 309 | case MCSymbolRefExpr::VK_TLSLD: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 310 | case MCSymbolRefExpr::VK_TLSLDM: |
| 311 | case MCSymbolRefExpr::VK_TPOFF: |
| 312 | case MCSymbolRefExpr::VK_DTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 313 | case MCSymbolRefExpr::VK_ARM_TLSGD: |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 314 | case MCSymbolRefExpr::VK_ARM_TPOFF: |
| 315 | case MCSymbolRefExpr::VK_ARM_GOTTPOFF: |
Bruno Cardoso Lopes | 3507d24 | 2011-10-25 18:13:20 +0000 | [diff] [blame] | 316 | case MCSymbolRefExpr::VK_Mips_TLSGD: |
| 317 | case MCSymbolRefExpr::VK_Mips_GOTTPREL: |
| 318 | case MCSymbolRefExpr::VK_Mips_TPREL_HI: |
| 319 | case MCSymbolRefExpr::VK_Mips_TPREL_LO: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 320 | break; |
| 321 | } |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 322 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol()); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 323 | MCELF::SetType(SD, ELF::STT_TLS); |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 324 | break; |
| 325 | } |
| 326 | |
| 327 | case MCExpr::Unary: |
| 328 | fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 333 | void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 334 | this->MCObjectStreamer::EmitInstToFragment(Inst); |
| 335 | MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 336 | |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 337 | for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) |
| 338 | fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void MCELFStreamer::EmitInstToData(const MCInst &Inst) { |
| 342 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 343 | |
| 344 | SmallVector<MCFixup, 4> Fixups; |
| 345 | SmallString<256> Code; |
| 346 | raw_svector_ostream VecOS(Code); |
| 347 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 348 | VecOS.flush(); |
| 349 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 350 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 351 | fixSymbolsInTLSFixups(Fixups[i].getValue()); |
| 352 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 353 | // Add the fixups and data. |
| 354 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 355 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 356 | DF->addFixup(Fixups[i]); |
| 357 | } |
| 358 | DF->getContents().append(Code.begin(), Code.end()); |
| 359 | } |
| 360 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 361 | void MCELFStreamer::FinishImpl() { |
Rafael Espindola | c25dad8 | 2011-05-10 03:14:15 +0000 | [diff] [blame] | 362 | EmitFrames(true); |
Rafael Espindola | c70a1d9 | 2010-11-01 17:07:14 +0000 | [diff] [blame] | 363 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 364 | for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), |
| 365 | e = LocalCommons.end(); |
| 366 | i != e; ++i) { |
| 367 | MCSymbolData *SD = i->SD; |
| 368 | uint64_t Size = i->Size; |
| 369 | unsigned ByteAlignment = i->ByteAlignment; |
| 370 | const MCSymbol &Symbol = SD->getSymbol(); |
| 371 | const MCSection &Section = Symbol.getSection(); |
| 372 | |
| 373 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section); |
| 374 | new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); |
| 375 | |
| 376 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
| 377 | SD->setFragment(F); |
| 378 | |
| 379 | // Update the maximum alignment of the section if necessary. |
| 380 | if (ByteAlignment > SectData.getAlignment()) |
| 381 | SectData.setAlignment(ByteAlignment); |
| 382 | } |
| 383 | |
Rafael Espindola | 99b4237 | 2012-01-07 03:13:18 +0000 | [diff] [blame] | 384 | this->MCObjectStreamer::FinishImpl(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 385 | } |
Tim Northover | 6eb3e87 | 2012-12-07 16:50:23 +0000 | [diff] [blame^] | 386 | void MCELFStreamer::EmitTCEntry(const MCSymbol &S) { |
Adhemerval Zanella | f35c62b | 2012-10-15 15:43:14 +0000 | [diff] [blame] | 387 | // Creates a R_PPC64_TOC relocation |
| 388 | MCObjectStreamer::EmitSymbolValue(&S, 8, 0); |
| 389 | } |
| 390 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 391 | MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 392 | raw_ostream &OS, MCCodeEmitter *CE, |
| 393 | bool RelaxAll, bool NoExecStack) { |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 394 | MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 395 | if (RelaxAll) |
| 396 | S->getAssembler().setRelaxAll(true); |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 397 | if (NoExecStack) |
| 398 | S->getAssembler().setNoExecStack(true); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 399 | return S; |
| 400 | } |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 401 | |
Tim Northover | 6eb3e87 | 2012-12-07 16:50:23 +0000 | [diff] [blame^] | 402 | void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 403 | llvm_unreachable("Generic ELF doesn't support this directive"); |
| 404 | } |
| 405 | |
Logan Chien | 6450165 | 2012-12-07 15:50:40 +0000 | [diff] [blame] | 406 | void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 407 | llvm_unreachable("ELF doesn't support this directive"); |
| 408 | } |
| 409 | |
| 410 | void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 411 | llvm_unreachable("ELF doesn't support this directive"); |
| 412 | } |
| 413 | |
| 414 | void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) { |
| 415 | llvm_unreachable("ELF doesn't support this directive"); |
| 416 | } |
| 417 | |
| 418 | void MCELFStreamer::EmitCOFFSymbolType(int Type) { |
| 419 | llvm_unreachable("ELF doesn't support this directive"); |
| 420 | } |
| 421 | |
| 422 | void MCELFStreamer::EndCOFFSymbolDef() { |
| 423 | llvm_unreachable("ELF doesn't support this directive"); |
| 424 | } |
| 425 | |
| 426 | void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol, |
| 427 | uint64_t Size, unsigned ByteAlignment) { |
| 428 | llvm_unreachable("ELF doesn't support this directive"); |
| 429 | } |
| 430 | |
| 431 | void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 432 | uint64_t Size, unsigned ByteAlignment) { |
| 433 | llvm_unreachable("ELF doesn't support this directive"); |
| 434 | } |