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 | |
Jan Sjödin | 24b17c6 | 2011-03-03 14:52:12 +0000 | [diff] [blame] | 14 | #include "MCELFStreamer.h" |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 15 | #include "MCELF.h" |
Jan Sjödin | 24b17c6 | 2011-03-03 14:52:12 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCStreamer.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCCodeEmitter.h" |
| 18 | #include "llvm/MC/MCELFSymbolFlags.h" |
| 19 | #include "llvm/MC/MCExpr.h" |
| 20 | #include "llvm/MC/MCInst.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSection.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCSymbol.h" |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCValue.h" |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame^] | 24 | #include "llvm/MC/MCAsmBackend.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Support/ELF.h" |
| 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
| 31 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 32 | void MCELFStreamer::InitSections() { |
| 33 | // This emulates the same behavior of GNU as. This makes it easier |
| 34 | // to compare the output as the major sections are in the same order. |
| 35 | SetSectionText(); |
| 36 | SetSectionData(); |
| 37 | SetSectionBss(); |
| 38 | SetSectionText(); |
| 39 | } |
| 40 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 41 | void MCELFStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | ba21024 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 42 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 43 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 44 | MCObjectStreamer::EmitLabel(Symbol); |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 45 | |
Rafael Espindola | e1a2587 | 2010-11-11 19:04:55 +0000 | [diff] [blame] | 46 | const MCSectionELF &Section = |
| 47 | static_cast<const MCSectionELF&>(Symbol->getSection()); |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 48 | MCSymbolData &SD = getAssembler().getSymbolData(*Symbol); |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 49 | if (Section.getFlags() & ELF::SHF_TLS) |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 50 | MCELF::SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 54 | switch (Flag) { |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 55 | case MCAF_SyntaxUnified: return; // no-op here. |
| 56 | case MCAF_Code16: return; // no-op here. |
Jim Grosbach | ba21957 | 2010-11-05 22:40:09 +0000 | [diff] [blame] | 57 | case MCAF_Code32: return; // no-op here. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 58 | case MCAF_SubsectionsViaSymbols: |
| 59 | getAssembler().setSubsectionsViaSymbols(true); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | assert(0 && "invalid assembler flag!"); |
| 64 | } |
| 65 | |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 66 | void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 67 | // FIXME: Anything needed here to flag the function as thumb? |
Rafael Espindola | 6469540 | 2011-05-16 16:17:21 +0000 | [diff] [blame] | 68 | |
| 69 | getAssembler().setIsThumbFunc(Func); |
| 70 | |
| 71 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func); |
| 72 | SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc); |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 75 | void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 76 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 77 | // MCObjectStreamer. |
| 78 | // FIXME: Lift context changes into super class. |
| 79 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 80 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 81 | } |
| 82 | |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 83 | void MCELFStreamer::ChangeSection(const MCSection *Section) { |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 84 | const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); |
| 85 | if (Grp) |
| 86 | getAssembler().getOrCreateSymbolData(*Grp); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 87 | this->MCObjectStreamer::ChangeSection(Section); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 90 | void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { |
| 91 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 92 | MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias); |
| 93 | AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 94 | const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext()); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 95 | Alias->setVariableValue(Value); |
| 96 | } |
| 97 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 98 | void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 99 | MCSymbolAttr Attribute) { |
| 100 | // Indirect symbols are handled differently, to match how 'as' handles |
| 101 | // them. This makes writing matching .o files easier. |
| 102 | if (Attribute == MCSA_IndirectSymbol) { |
| 103 | // Note that we intentionally cannot use the symbol data here; this is |
| 104 | // important for matching the string table that 'as' generates. |
| 105 | IndirectSymbolData ISD; |
| 106 | ISD.Symbol = Symbol; |
| 107 | ISD.SectionData = getCurrentSectionData(); |
| 108 | getAssembler().getIndirectSymbols().push_back(ISD); |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | // Adding a symbol attribute always introduces the symbol, note that an |
| 113 | // important side effect of calling getOrCreateSymbolData here is to register |
| 114 | // the symbol with the assembler. |
| 115 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 116 | |
| 117 | // The implementation of symbol attributes is designed to match 'as', but it |
| 118 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 119 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 120 | // |
| 121 | // In the future it might be worth trying to make these operations more well |
| 122 | // defined. |
| 123 | switch (Attribute) { |
| 124 | case MCSA_LazyReference: |
| 125 | case MCSA_Reference: |
| 126 | case MCSA_NoDeadStrip: |
Kevin Enderby | e8e98d7 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 127 | case MCSA_SymbolResolver: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 128 | case MCSA_PrivateExtern: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 129 | case MCSA_WeakDefinition: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 130 | case MCSA_WeakDefAutoPrivate: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 131 | case MCSA_Invalid: |
| 132 | case MCSA_ELF_TypeIndFunction: |
| 133 | case MCSA_IndirectSymbol: |
| 134 | assert(0 && "Invalid symbol attribute for ELF!"); |
| 135 | break; |
| 136 | |
Rafael Espindola | 7e528a1 | 2010-11-14 01:34:31 +0000 | [diff] [blame] | 137 | case MCSA_ELF_TypeGnuUniqueObject: |
| 138 | // Ignore for now. |
| 139 | break; |
| 140 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 141 | case MCSA_Global: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 142 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 143 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 144 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 145 | break; |
| 146 | |
Benjamin Kramer | 230c274 | 2010-09-02 17:18:32 +0000 | [diff] [blame] | 147 | case MCSA_WeakReference: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 148 | case MCSA_Weak: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 149 | MCELF::SetBinding(SD, ELF::STB_WEAK); |
Rafael Espindola | 3223f19 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 150 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 151 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 152 | break; |
| 153 | |
| 154 | case MCSA_Local: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 155 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 156 | SD.setExternal(false); |
| 157 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 158 | break; |
| 159 | |
| 160 | case MCSA_ELF_TypeFunction: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 161 | MCELF::SetType(SD, ELF::STT_FUNC); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 162 | break; |
| 163 | |
| 164 | case MCSA_ELF_TypeObject: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 165 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 166 | break; |
| 167 | |
| 168 | case MCSA_ELF_TypeTLS: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 169 | MCELF::SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 170 | break; |
| 171 | |
| 172 | case MCSA_ELF_TypeCommon: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 173 | MCELF::SetType(SD, ELF::STT_COMMON); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 174 | break; |
| 175 | |
| 176 | case MCSA_ELF_TypeNoType: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 177 | MCELF::SetType(SD, ELF::STT_NOTYPE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 178 | break; |
| 179 | |
| 180 | case MCSA_Protected: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 181 | MCELF::SetVisibility(SD, ELF::STV_PROTECTED); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 182 | break; |
| 183 | |
| 184 | case MCSA_Hidden: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 185 | MCELF::SetVisibility(SD, ELF::STV_HIDDEN); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 186 | break; |
| 187 | |
| 188 | case MCSA_Internal: |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 189 | MCELF::SetVisibility(SD, ELF::STV_INTERNAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 190 | break; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 195 | unsigned ByteAlignment) { |
| 196 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 197 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 198 | if (!BindingExplicitlySet.count(Symbol)) { |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 199 | MCELF::SetBinding(SD, ELF::STB_GLOBAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 200 | SD.setExternal(true); |
| 201 | } |
| 202 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 203 | MCELF::SetType(SD, ELF::STT_OBJECT); |
Rafael Espindola | 55d02f3 | 2010-11-14 21:11:16 +0000 | [diff] [blame] | 204 | |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 205 | if (MCELF::GetBinding(SD) == ELF_STB_Local) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 206 | const MCSection *Section = getAssembler().getContext().getELFSection(".bss", |
Rafael Espindola | c85dca6 | 2011-01-23 04:28:49 +0000 | [diff] [blame] | 207 | ELF::SHT_NOBITS, |
Rafael Espindola | 1c13026 | 2011-01-23 04:43:11 +0000 | [diff] [blame] | 208 | ELF::SHF_WRITE | |
| 209 | ELF::SHF_ALLOC, |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 210 | SectionKind::getBSS()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 211 | Symbol->setSection(*Section); |
Rafael Espindola | 1963572 | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 212 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 213 | struct LocalCommon L = {&SD, Size, ByteAlignment}; |
| 214 | LocalCommons.push_back(L); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 215 | } else { |
| 216 | SD.setCommon(Size, ByteAlignment); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 219 | SD.setSize(MCConstantExpr::Create(Size, getContext())); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 220 | } |
| 221 | |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 222 | void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 223 | // FIXME: Should this be caught and done earlier? |
| 224 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 225 | MCELF::SetBinding(SD, ELF::STB_LOCAL); |
Jason W Kim | f13743b | 2010-12-16 03:12:17 +0000 | [diff] [blame] | 226 | SD.setExternal(false); |
| 227 | BindingExplicitlySet.insert(Symbol); |
| 228 | // FIXME: ByteAlignment is not needed here, but is required. |
| 229 | EmitCommonSymbol(Symbol, Size, 1); |
| 230 | } |
| 231 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 232 | void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 233 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 234 | // MCObjectStreamer. |
| 235 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 236 | } |
| 237 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 238 | void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 239 | int64_t Value, unsigned ValueSize, |
| 240 | unsigned MaxBytesToEmit) { |
| 241 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 242 | // MCObjectStreamer. |
| 243 | if (MaxBytesToEmit == 0) |
| 244 | MaxBytesToEmit = ByteAlignment; |
| 245 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 246 | getCurrentSectionData()); |
| 247 | |
| 248 | // Update the maximum alignment on the current section if necessary. |
| 249 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 250 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 251 | } |
| 252 | |
| 253 | void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 254 | unsigned MaxBytesToEmit) { |
| 255 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 256 | // MCObjectStreamer. |
| 257 | if (MaxBytesToEmit == 0) |
| 258 | MaxBytesToEmit = ByteAlignment; |
| 259 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
| 260 | getCurrentSectionData()); |
| 261 | F->setEmitNops(true); |
| 262 | |
| 263 | // Update the maximum alignment on the current section if necessary. |
| 264 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 265 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 266 | } |
| 267 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 268 | // Add a symbol for the file name of this module. This is the second |
| 269 | // entry in the module's symbol table (the first being the null symbol). |
| 270 | void MCELFStreamer::EmitFileDirective(StringRef Filename) { |
| 271 | MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename); |
Rafael Espindola | 7768a9d | 2011-02-16 01:08:29 +0000 | [diff] [blame] | 272 | Symbol->setSection(*getCurrentSection()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 273 | Symbol->setAbsolute(); |
| 274 | |
| 275 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 276 | |
| 277 | SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default); |
| 278 | } |
| 279 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 280 | void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { |
| 281 | switch (expr->getKind()) { |
| 282 | case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); |
| 283 | case MCExpr::Constant: |
| 284 | break; |
| 285 | |
| 286 | case MCExpr::Binary: { |
| 287 | const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); |
| 288 | fixSymbolsInTLSFixups(be->getLHS()); |
| 289 | fixSymbolsInTLSFixups(be->getRHS()); |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | case MCExpr::SymbolRef: { |
| 294 | const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 295 | switch (symRef.getKind()) { |
| 296 | default: |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 297 | return; |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 298 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 299 | case MCSymbolRefExpr::VK_INDNTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 300 | case MCSymbolRefExpr::VK_NTPOFF: |
| 301 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 302 | case MCSymbolRefExpr::VK_TLSGD: |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 303 | case MCSymbolRefExpr::VK_TLSLD: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 304 | case MCSymbolRefExpr::VK_TLSLDM: |
| 305 | case MCSymbolRefExpr::VK_TPOFF: |
| 306 | case MCSymbolRefExpr::VK_DTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 307 | case MCSymbolRefExpr::VK_ARM_TLSGD: |
Joerg Sonnenberger | d02c8b6 | 2011-03-17 00:35:10 +0000 | [diff] [blame] | 308 | case MCSymbolRefExpr::VK_ARM_TPOFF: |
| 309 | case MCSymbolRefExpr::VK_ARM_GOTTPOFF: |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 310 | break; |
| 311 | } |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 312 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol()); |
Jan Sjödin | 2ddfd95 | 2011-02-28 21:45:04 +0000 | [diff] [blame] | 313 | MCELF::SetType(SD, ELF::STT_TLS); |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 314 | break; |
| 315 | } |
| 316 | |
| 317 | case MCExpr::Unary: |
| 318 | fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 323 | void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 324 | this->MCObjectStreamer::EmitInstToFragment(Inst); |
| 325 | MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 326 | |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 327 | for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) |
| 328 | fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void MCELFStreamer::EmitInstToData(const MCInst &Inst) { |
| 332 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 333 | |
| 334 | SmallVector<MCFixup, 4> Fixups; |
| 335 | SmallString<256> Code; |
| 336 | raw_svector_ostream VecOS(Code); |
| 337 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 338 | VecOS.flush(); |
| 339 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 340 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 341 | fixSymbolsInTLSFixups(Fixups[i].getValue()); |
| 342 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 343 | // Add the fixups and data. |
| 344 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 345 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 346 | DF->addFixup(Fixups[i]); |
| 347 | } |
| 348 | DF->getContents().append(Code.begin(), Code.end()); |
| 349 | } |
| 350 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 351 | void MCELFStreamer::Finish() { |
Rafael Espindola | c25dad8 | 2011-05-10 03:14:15 +0000 | [diff] [blame] | 352 | EmitFrames(true); |
Rafael Espindola | c70a1d9 | 2010-11-01 17:07:14 +0000 | [diff] [blame] | 353 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 354 | for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), |
| 355 | e = LocalCommons.end(); |
| 356 | i != e; ++i) { |
| 357 | MCSymbolData *SD = i->SD; |
| 358 | uint64_t Size = i->Size; |
| 359 | unsigned ByteAlignment = i->ByteAlignment; |
| 360 | const MCSymbol &Symbol = SD->getSymbol(); |
| 361 | const MCSection &Section = Symbol.getSection(); |
| 362 | |
| 363 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section); |
| 364 | new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); |
| 365 | |
| 366 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
| 367 | SD->setFragment(F); |
| 368 | |
| 369 | // Update the maximum alignment of the section if necessary. |
| 370 | if (ByteAlignment > SectData.getAlignment()) |
| 371 | SectData.setAlignment(ByteAlignment); |
| 372 | } |
| 373 | |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 374 | this->MCObjectStreamer::Finish(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame^] | 377 | MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 378 | raw_ostream &OS, MCCodeEmitter *CE, |
| 379 | bool RelaxAll, bool NoExecStack) { |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame^] | 380 | MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 381 | if (RelaxAll) |
| 382 | S->getAssembler().setRelaxAll(true); |
Rafael Espindola | 96aa78c | 2011-01-23 17:55:27 +0000 | [diff] [blame] | 383 | if (NoExecStack) |
| 384 | S->getAssembler().setNoExecStack(true); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 385 | return S; |
| 386 | } |