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 | |
| 14 | #include "llvm/MC/MCStreamer.h" |
| 15 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallPtrSet.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCAssembler.h" |
| 18 | #include "llvm/MC/MCContext.h" |
| 19 | #include "llvm/MC/MCCodeEmitter.h" |
| 20 | #include "llvm/MC/MCELFSymbolFlags.h" |
| 21 | #include "llvm/MC/MCExpr.h" |
| 22 | #include "llvm/MC/MCInst.h" |
| 23 | #include "llvm/MC/MCObjectStreamer.h" |
| 24 | #include "llvm/MC/MCSection.h" |
| 25 | #include "llvm/MC/MCSectionELF.h" |
| 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" |
| 32 | #include "llvm/Target/TargetAsmBackend.h" |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetAsmInfo.h" |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace llvm; |
| 36 | |
| 37 | namespace { |
| 38 | |
Rafael Espindola | e1a2587 | 2010-11-11 19:04:55 +0000 | [diff] [blame] | 39 | static void SetBinding(MCSymbolData &SD, unsigned Binding) { |
| 40 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 41 | Binding == ELF::STB_WEAK); |
| 42 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift); |
| 43 | SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift)); |
| 44 | } |
| 45 | |
| 46 | static unsigned GetBinding(const MCSymbolData &SD) { |
| 47 | uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift; |
| 48 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 49 | Binding == ELF::STB_WEAK); |
| 50 | return Binding; |
| 51 | } |
| 52 | |
| 53 | static void SetType(MCSymbolData &SD, unsigned Type) { |
| 54 | assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT || |
| 55 | Type == ELF::STT_FUNC || Type == ELF::STT_SECTION || |
| 56 | Type == ELF::STT_FILE || Type == ELF::STT_COMMON || |
| 57 | Type == ELF::STT_TLS); |
| 58 | |
| 59 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift); |
| 60 | SD.setFlags(OtherFlags | (Type << ELF_STT_Shift)); |
| 61 | } |
| 62 | |
| 63 | static void SetVisibility(MCSymbolData &SD, unsigned Visibility) { |
| 64 | assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL || |
| 65 | Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED); |
| 66 | |
| 67 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift); |
| 68 | SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift)); |
| 69 | } |
| 70 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 71 | class MCELFStreamer : public MCObjectStreamer { |
| 72 | public: |
| 73 | MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 74 | raw_ostream &OS, MCCodeEmitter *Emitter) |
Rafael Espindola | 85f2ecc | 2010-12-07 00:27:36 +0000 | [diff] [blame] | 75 | : MCObjectStreamer(Context, TAB, OS, Emitter) {} |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 76 | |
| 77 | ~MCELFStreamer() {} |
| 78 | |
| 79 | /// @name MCStreamer Interface |
| 80 | /// @{ |
| 81 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 82 | virtual void InitSections(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 83 | virtual void EmitLabel(MCSymbol *Symbol); |
| 84 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 85 | virtual void EmitThumbFunc(MCSymbol *Func); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 86 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 87 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 88 | virtual void SwitchSection(const MCSection *Section); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 89 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
| 90 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 91 | assert(0 && "ELF doesn't support this directive"); |
| 92 | } |
| 93 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 94 | unsigned ByteAlignment); |
| 95 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 96 | assert(0 && "ELF doesn't support this directive"); |
| 97 | } |
| 98 | |
| 99 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 100 | assert(0 && "ELF doesn't support this directive"); |
| 101 | } |
| 102 | |
| 103 | virtual void EmitCOFFSymbolType(int Type) { |
| 104 | assert(0 && "ELF doesn't support this directive"); |
| 105 | } |
| 106 | |
| 107 | virtual void EndCOFFSymbolDef() { |
| 108 | assert(0 && "ELF doesn't support this directive"); |
| 109 | } |
| 110 | |
| 111 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 112 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 113 | SD.setSize(Value); |
| 114 | } |
| 115 | |
| 116 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 117 | assert(0 && "ELF doesn't support this directive"); |
| 118 | } |
| 119 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
| 120 | unsigned Size = 0, unsigned ByteAlignment = 0) { |
| 121 | assert(0 && "ELF doesn't support this directive"); |
| 122 | } |
| 123 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 124 | uint64_t Size, unsigned ByteAlignment = 0) { |
| 125 | assert(0 && "ELF doesn't support this directive"); |
| 126 | } |
| 127 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 128 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 129 | unsigned ValueSize = 1, |
| 130 | unsigned MaxBytesToEmit = 0); |
| 131 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 132 | unsigned MaxBytesToEmit = 0); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 133 | |
| 134 | virtual void EmitFileDirective(StringRef Filename); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 135 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 136 | virtual void Finish(); |
| 137 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 138 | private: |
Rafael Espindola | f89671d | 2010-11-01 16:27:31 +0000 | [diff] [blame] | 139 | virtual void EmitInstToFragment(const MCInst &Inst); |
| 140 | virtual void EmitInstToData(const MCInst &Inst); |
| 141 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 142 | void fixSymbolsInTLSFixups(const MCExpr *expr); |
| 143 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 144 | struct LocalCommon { |
| 145 | MCSymbolData *SD; |
| 146 | uint64_t Size; |
| 147 | unsigned ByteAlignment; |
| 148 | }; |
| 149 | std::vector<LocalCommon> LocalCommons; |
| 150 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 151 | SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 152 | /// @} |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 153 | void SetSection(StringRef Section, unsigned Type, unsigned Flags, |
| 154 | SectionKind Kind) { |
| 155 | SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); |
| 156 | } |
| 157 | |
| 158 | void SetSectionData() { |
| 159 | SetSection(".data", MCSectionELF::SHT_PROGBITS, |
| 160 | MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, |
| 161 | SectionKind::getDataRel()); |
| 162 | EmitCodeAlignment(4, 0); |
| 163 | } |
| 164 | void SetSectionText() { |
| 165 | SetSection(".text", MCSectionELF::SHT_PROGBITS, |
| 166 | MCSectionELF::SHF_EXECINSTR | |
| 167 | MCSectionELF::SHF_ALLOC, SectionKind::getText()); |
| 168 | EmitCodeAlignment(4, 0); |
| 169 | } |
| 170 | void SetSectionBss() { |
| 171 | SetSection(".bss", MCSectionELF::SHT_NOBITS, |
| 172 | MCSectionELF::SHF_WRITE | |
| 173 | MCSectionELF::SHF_ALLOC, SectionKind::getBSS()); |
| 174 | EmitCodeAlignment(4, 0); |
| 175 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } // end anonymous namespace. |
| 179 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 180 | void MCELFStreamer::InitSections() { |
| 181 | // This emulates the same behavior of GNU as. This makes it easier |
| 182 | // to compare the output as the major sections are in the same order. |
| 183 | SetSectionText(); |
| 184 | SetSectionData(); |
| 185 | SetSectionBss(); |
| 186 | SetSectionText(); |
| 187 | } |
| 188 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 189 | void MCELFStreamer::EmitLabel(MCSymbol *Symbol) { |
Rafael Espindola | ba21024 | 2010-11-28 16:22:59 +0000 | [diff] [blame] | 190 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 191 | |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 192 | MCObjectStreamer::EmitLabel(Symbol); |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 193 | |
Rafael Espindola | e1a2587 | 2010-11-11 19:04:55 +0000 | [diff] [blame] | 194 | const MCSectionELF &Section = |
| 195 | static_cast<const MCSectionELF&>(Symbol->getSection()); |
Rafael Espindola | ea4afa9 | 2010-11-28 17:18:55 +0000 | [diff] [blame] | 196 | MCSymbolData &SD = getAssembler().getSymbolData(*Symbol); |
Rafael Espindola | e1a2587 | 2010-11-11 19:04:55 +0000 | [diff] [blame] | 197 | if (Section.getFlags() & MCSectionELF::SHF_TLS) |
| 198 | SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 202 | switch (Flag) { |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 203 | case MCAF_SyntaxUnified: return; // no-op here. |
| 204 | case MCAF_Code16: return; // no-op here. |
Jim Grosbach | ba21957 | 2010-11-05 22:40:09 +0000 | [diff] [blame] | 205 | case MCAF_Code32: return; // no-op here. |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 206 | case MCAF_SubsectionsViaSymbols: |
| 207 | getAssembler().setSubsectionsViaSymbols(true); |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | assert(0 && "invalid assembler flag!"); |
| 212 | } |
| 213 | |
Jim Grosbach | ce79299 | 2010-11-05 22:08:08 +0000 | [diff] [blame] | 214 | void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { |
| 215 | // FIXME: Anything needed here to flag the function as thumb? |
| 216 | } |
| 217 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 218 | void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 219 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 220 | // MCObjectStreamer. |
| 221 | // FIXME: Lift context changes into super class. |
| 222 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 223 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 224 | } |
| 225 | |
Rafael Espindola | 1f4f9e3 | 2010-11-14 04:17:37 +0000 | [diff] [blame] | 226 | void MCELFStreamer::SwitchSection(const MCSection *Section) { |
| 227 | const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); |
| 228 | if (Grp) |
| 229 | getAssembler().getOrCreateSymbolData(*Grp); |
| 230 | this->MCObjectStreamer::SwitchSection(Section); |
| 231 | } |
| 232 | |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 233 | void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { |
| 234 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 235 | MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias); |
| 236 | AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref); |
Rafael Espindola | 94ed5fc | 2010-11-15 16:33:49 +0000 | [diff] [blame] | 237 | const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext()); |
Rafael Espindola | 484291c | 2010-11-01 14:28:48 +0000 | [diff] [blame] | 238 | Alias->setVariableValue(Value); |
| 239 | } |
| 240 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 241 | void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 242 | MCSymbolAttr Attribute) { |
| 243 | // Indirect symbols are handled differently, to match how 'as' handles |
| 244 | // them. This makes writing matching .o files easier. |
| 245 | if (Attribute == MCSA_IndirectSymbol) { |
| 246 | // Note that we intentionally cannot use the symbol data here; this is |
| 247 | // important for matching the string table that 'as' generates. |
| 248 | IndirectSymbolData ISD; |
| 249 | ISD.Symbol = Symbol; |
| 250 | ISD.SectionData = getCurrentSectionData(); |
| 251 | getAssembler().getIndirectSymbols().push_back(ISD); |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | // Adding a symbol attribute always introduces the symbol, note that an |
| 256 | // important side effect of calling getOrCreateSymbolData here is to register |
| 257 | // the symbol with the assembler. |
| 258 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 259 | |
| 260 | // The implementation of symbol attributes is designed to match 'as', but it |
| 261 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 262 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 263 | // |
| 264 | // In the future it might be worth trying to make these operations more well |
| 265 | // defined. |
| 266 | switch (Attribute) { |
| 267 | case MCSA_LazyReference: |
| 268 | case MCSA_Reference: |
| 269 | case MCSA_NoDeadStrip: |
Kevin Enderby | e8e98d7 | 2010-11-19 18:39:33 +0000 | [diff] [blame] | 270 | case MCSA_SymbolResolver: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 271 | case MCSA_PrivateExtern: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 272 | case MCSA_WeakDefinition: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 273 | case MCSA_WeakDefAutoPrivate: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 274 | case MCSA_Invalid: |
| 275 | case MCSA_ELF_TypeIndFunction: |
| 276 | case MCSA_IndirectSymbol: |
| 277 | assert(0 && "Invalid symbol attribute for ELF!"); |
| 278 | break; |
| 279 | |
Rafael Espindola | 7e528a1 | 2010-11-14 01:34:31 +0000 | [diff] [blame] | 280 | case MCSA_ELF_TypeGnuUniqueObject: |
| 281 | // Ignore for now. |
| 282 | break; |
| 283 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 284 | case MCSA_Global: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 285 | SetBinding(SD, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 286 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 287 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 288 | break; |
| 289 | |
Benjamin Kramer | 230c274 | 2010-09-02 17:18:32 +0000 | [diff] [blame] | 290 | case MCSA_WeakReference: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 291 | case MCSA_Weak: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 292 | SetBinding(SD, ELF::STB_WEAK); |
Rafael Espindola | 3223f19 | 2010-10-06 16:47:31 +0000 | [diff] [blame] | 293 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 294 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 295 | break; |
| 296 | |
| 297 | case MCSA_Local: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 298 | SetBinding(SD, ELF::STB_LOCAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 299 | SD.setExternal(false); |
| 300 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 301 | break; |
| 302 | |
| 303 | case MCSA_ELF_TypeFunction: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 304 | SetType(SD, ELF::STT_FUNC); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 305 | break; |
| 306 | |
| 307 | case MCSA_ELF_TypeObject: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 308 | SetType(SD, ELF::STT_OBJECT); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 309 | break; |
| 310 | |
| 311 | case MCSA_ELF_TypeTLS: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 312 | SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 313 | break; |
| 314 | |
| 315 | case MCSA_ELF_TypeCommon: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 316 | SetType(SD, ELF::STT_COMMON); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 317 | break; |
| 318 | |
| 319 | case MCSA_ELF_TypeNoType: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 320 | SetType(SD, ELF::STT_NOTYPE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 321 | break; |
| 322 | |
| 323 | case MCSA_Protected: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 324 | SetVisibility(SD, ELF::STV_PROTECTED); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 325 | break; |
| 326 | |
| 327 | case MCSA_Hidden: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 328 | SetVisibility(SD, ELF::STV_HIDDEN); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 329 | break; |
| 330 | |
| 331 | case MCSA_Internal: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 332 | SetVisibility(SD, ELF::STV_INTERNAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 338 | unsigned ByteAlignment) { |
| 339 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 340 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 341 | if (!BindingExplicitlySet.count(Symbol)) { |
| 342 | SetBinding(SD, ELF::STB_GLOBAL); |
| 343 | SD.setExternal(true); |
| 344 | } |
| 345 | |
Rafael Espindola | 55d02f3 | 2010-11-14 21:11:16 +0000 | [diff] [blame] | 346 | SetType(SD, ELF::STT_OBJECT); |
| 347 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 348 | if (GetBinding(SD) == ELF_STB_Local) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 349 | const MCSection *Section = getAssembler().getContext().getELFSection(".bss", |
| 350 | MCSectionELF::SHT_NOBITS, |
| 351 | MCSectionELF::SHF_WRITE | |
| 352 | MCSectionELF::SHF_ALLOC, |
| 353 | SectionKind::getBSS()); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 354 | Symbol->setSection(*Section); |
Rafael Espindola | 1963572 | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 355 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 356 | struct LocalCommon L = {&SD, Size, ByteAlignment}; |
| 357 | LocalCommons.push_back(L); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 358 | } else { |
| 359 | SD.setCommon(Size, ByteAlignment); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 362 | SD.setSize(MCConstantExpr::Create(Size, getContext())); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 366 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 367 | // MCObjectStreamer. |
| 368 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 369 | } |
| 370 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 371 | void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 372 | int64_t Value, unsigned ValueSize, |
| 373 | unsigned MaxBytesToEmit) { |
| 374 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 375 | // MCObjectStreamer. |
| 376 | if (MaxBytesToEmit == 0) |
| 377 | MaxBytesToEmit = ByteAlignment; |
| 378 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 379 | getCurrentSectionData()); |
| 380 | |
| 381 | // Update the maximum alignment on the current section if necessary. |
| 382 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 383 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 384 | } |
| 385 | |
| 386 | void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 387 | unsigned MaxBytesToEmit) { |
| 388 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 389 | // MCObjectStreamer. |
| 390 | if (MaxBytesToEmit == 0) |
| 391 | MaxBytesToEmit = ByteAlignment; |
| 392 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
| 393 | getCurrentSectionData()); |
| 394 | F->setEmitNops(true); |
| 395 | |
| 396 | // Update the maximum alignment on the current section if necessary. |
| 397 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 398 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 399 | } |
| 400 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 401 | // Add a symbol for the file name of this module. This is the second |
| 402 | // entry in the module's symbol table (the first being the null symbol). |
| 403 | void MCELFStreamer::EmitFileDirective(StringRef Filename) { |
| 404 | MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename); |
| 405 | Symbol->setSection(*CurSection); |
| 406 | Symbol->setAbsolute(); |
| 407 | |
| 408 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 409 | |
| 410 | SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default); |
| 411 | } |
| 412 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 413 | void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { |
| 414 | switch (expr->getKind()) { |
| 415 | case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); |
| 416 | case MCExpr::Constant: |
| 417 | break; |
| 418 | |
| 419 | case MCExpr::Binary: { |
| 420 | const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); |
| 421 | fixSymbolsInTLSFixups(be->getLHS()); |
| 422 | fixSymbolsInTLSFixups(be->getRHS()); |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | case MCExpr::SymbolRef: { |
| 427 | const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 428 | switch (symRef.getKind()) { |
| 429 | default: |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 430 | return; |
Rafael Espindola | bf8209d | 2010-11-24 18:51:21 +0000 | [diff] [blame] | 431 | case MCSymbolRefExpr::VK_NTPOFF: |
| 432 | case MCSymbolRefExpr::VK_GOTNTPOFF: |
| 433 | case MCSymbolRefExpr::VK_TLSGD: |
| 434 | case MCSymbolRefExpr::VK_TLSLDM: |
| 435 | case MCSymbolRefExpr::VK_TPOFF: |
| 436 | case MCSymbolRefExpr::VK_DTPOFF: |
| 437 | case MCSymbolRefExpr::VK_GOTTPOFF: |
| 438 | case MCSymbolRefExpr::VK_TLSLD: |
| 439 | case MCSymbolRefExpr::VK_ARM_TLSGD: |
| 440 | break; |
| 441 | } |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 442 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol()); |
| 443 | SetType(SD, ELF::STT_TLS); |
| 444 | break; |
| 445 | } |
| 446 | |
| 447 | case MCExpr::Unary: |
| 448 | fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); |
| 449 | break; |
| 450 | } |
| 451 | } |
| 452 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 453 | void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 454 | this->MCObjectStreamer::EmitInstToFragment(Inst); |
| 455 | MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 456 | |
Rafael Espindola | dedb045 | 2010-12-02 05:44:06 +0000 | [diff] [blame] | 457 | for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) |
| 458 | fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | void MCELFStreamer::EmitInstToData(const MCInst &Inst) { |
| 462 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 463 | |
| 464 | SmallVector<MCFixup, 4> Fixups; |
| 465 | SmallString<256> Code; |
| 466 | raw_svector_ostream VecOS(Code); |
| 467 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 468 | VecOS.flush(); |
| 469 | |
Rafael Espindola | 9755127 | 2010-11-24 02:19:40 +0000 | [diff] [blame] | 470 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) |
| 471 | fixSymbolsInTLSFixups(Fixups[i].getValue()); |
| 472 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 473 | // Add the fixups and data. |
| 474 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 475 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 476 | DF->addFixup(Fixups[i]); |
| 477 | } |
| 478 | DF->getContents().append(Code.begin(), Code.end()); |
| 479 | } |
| 480 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 481 | void MCELFStreamer::Finish() { |
Rafael Espindola | 89b9372 | 2010-12-10 07:39:47 +0000 | [diff] [blame] | 482 | if (!FrameInfos.empty()) |
| 483 | MCDwarfFrameEmitter::Emit(*this); |
Rafael Espindola | c70a1d9 | 2010-11-01 17:07:14 +0000 | [diff] [blame] | 484 | |
Rafael Espindola | 9e3922e | 2010-09-29 14:52:01 +0000 | [diff] [blame] | 485 | for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), |
| 486 | e = LocalCommons.end(); |
| 487 | i != e; ++i) { |
| 488 | MCSymbolData *SD = i->SD; |
| 489 | uint64_t Size = i->Size; |
| 490 | unsigned ByteAlignment = i->ByteAlignment; |
| 491 | const MCSymbol &Symbol = SD->getSymbol(); |
| 492 | const MCSection &Section = Symbol.getSection(); |
| 493 | |
| 494 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section); |
| 495 | new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); |
| 496 | |
| 497 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
| 498 | SD->setFragment(F); |
| 499 | |
| 500 | // Update the maximum alignment of the section if necessary. |
| 501 | if (ByteAlignment > SectData.getAlignment()) |
| 502 | SectData.setAlignment(ByteAlignment); |
| 503 | } |
| 504 | |
Rafael Espindola | 73ffea4 | 2010-09-25 05:42:19 +0000 | [diff] [blame] | 505 | this->MCObjectStreamer::Finish(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 509 | raw_ostream &OS, MCCodeEmitter *CE, |
| 510 | bool RelaxAll) { |
| 511 | MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE); |
| 512 | if (RelaxAll) |
| 513 | S->getAssembler().setRelaxAll(true); |
| 514 | return S; |
| 515 | } |