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