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