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" |
| 27 | #include "llvm/Support/Debug.h" |
| 28 | #include "llvm/Support/ELF.h" |
| 29 | #include "llvm/Support/ErrorHandling.h" |
| 30 | #include "llvm/Support/raw_ostream.h" |
| 31 | #include "llvm/Target/TargetAsmBackend.h" |
| 32 | |
| 33 | using namespace llvm; |
| 34 | |
| 35 | namespace { |
| 36 | |
| 37 | class MCELFStreamer : public MCObjectStreamer { |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 38 | void EmitInstToFragment(const MCInst &Inst); |
| 39 | void EmitInstToData(const MCInst &Inst); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 40 | public: |
| 41 | MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 42 | raw_ostream &OS, MCCodeEmitter *Emitter) |
| 43 | : MCObjectStreamer(Context, TAB, OS, Emitter) {} |
| 44 | |
| 45 | ~MCELFStreamer() {} |
| 46 | |
| 47 | /// @name MCStreamer Interface |
| 48 | /// @{ |
| 49 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 50 | virtual void InitSections(); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 51 | virtual void EmitLabel(MCSymbol *Symbol); |
| 52 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
| 53 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
| 54 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
| 55 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 56 | assert(0 && "ELF doesn't support this directive"); |
| 57 | } |
| 58 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 59 | unsigned ByteAlignment); |
| 60 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 61 | assert(0 && "ELF doesn't support this directive"); |
| 62 | } |
| 63 | |
| 64 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 65 | assert(0 && "ELF doesn't support this directive"); |
| 66 | } |
| 67 | |
| 68 | virtual void EmitCOFFSymbolType(int Type) { |
| 69 | assert(0 && "ELF doesn't support this directive"); |
| 70 | } |
| 71 | |
| 72 | virtual void EndCOFFSymbolDef() { |
| 73 | assert(0 && "ELF doesn't support this directive"); |
| 74 | } |
| 75 | |
| 76 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 77 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 78 | SD.setSize(Value); |
| 79 | } |
| 80 | |
| 81 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) { |
| 82 | assert(0 && "ELF doesn't support this directive"); |
| 83 | } |
| 84 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
| 85 | unsigned Size = 0, unsigned ByteAlignment = 0) { |
| 86 | assert(0 && "ELF doesn't support this directive"); |
| 87 | } |
| 88 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 89 | uint64_t Size, unsigned ByteAlignment = 0) { |
| 90 | assert(0 && "ELF doesn't support this directive"); |
| 91 | } |
| 92 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
| 93 | virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace); |
| 94 | virtual void EmitGPRel32Value(const MCExpr *Value) { |
| 95 | assert(0 && "ELF doesn't support this directive"); |
| 96 | } |
| 97 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 98 | unsigned ValueSize = 1, |
| 99 | unsigned MaxBytesToEmit = 0); |
| 100 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 101 | unsigned MaxBytesToEmit = 0); |
| 102 | virtual void EmitValueToOffset(const MCExpr *Offset, |
| 103 | unsigned char Value = 0); |
| 104 | |
| 105 | virtual void EmitFileDirective(StringRef Filename); |
| 106 | virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) { |
| 107 | DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n"); |
| 108 | } |
| 109 | |
| 110 | virtual void EmitInstruction(const MCInst &Inst); |
| 111 | virtual void Finish(); |
| 112 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 113 | private: |
| 114 | SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 115 | /// @} |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 116 | void SetSection(StringRef Section, unsigned Type, unsigned Flags, |
| 117 | SectionKind Kind) { |
| 118 | SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); |
| 119 | } |
| 120 | |
| 121 | void SetSectionData() { |
| 122 | SetSection(".data", MCSectionELF::SHT_PROGBITS, |
| 123 | MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC, |
| 124 | SectionKind::getDataRel()); |
| 125 | EmitCodeAlignment(4, 0); |
| 126 | } |
| 127 | void SetSectionText() { |
| 128 | SetSection(".text", MCSectionELF::SHT_PROGBITS, |
| 129 | MCSectionELF::SHF_EXECINSTR | |
| 130 | MCSectionELF::SHF_ALLOC, SectionKind::getText()); |
| 131 | EmitCodeAlignment(4, 0); |
| 132 | } |
| 133 | void SetSectionBss() { |
| 134 | SetSection(".bss", MCSectionELF::SHT_NOBITS, |
| 135 | MCSectionELF::SHF_WRITE | |
| 136 | MCSectionELF::SHF_ALLOC, SectionKind::getBSS()); |
| 137 | EmitCodeAlignment(4, 0); |
| 138 | } |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
| 141 | } // end anonymous namespace. |
| 142 | |
Rafael Espindola | d80781b | 2010-09-15 21:48:40 +0000 | [diff] [blame] | 143 | void MCELFStreamer::InitSections() { |
| 144 | // This emulates the same behavior of GNU as. This makes it easier |
| 145 | // to compare the output as the major sections are in the same order. |
| 146 | SetSectionText(); |
| 147 | SetSectionData(); |
| 148 | SetSectionBss(); |
| 149 | SetSectionText(); |
| 150 | } |
| 151 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 152 | void MCELFStreamer::EmitLabel(MCSymbol *Symbol) { |
| 153 | assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); |
| 154 | |
| 155 | // FIXME: This is wasteful, we don't necessarily need to create a data |
| 156 | // fragment. Instead, we should mark the symbol as pointing into the data |
| 157 | // fragment if it exists, otherwise we should just queue the label and set its |
| 158 | // fragment pointer when we emit the next fragment. |
| 159 | MCDataFragment *F = getOrCreateDataFragment(); |
| 160 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 161 | assert(!SD.getFragment() && "Unexpected fragment on symbol data!"); |
| 162 | SD.setFragment(F); |
| 163 | SD.setOffset(F->getContents().size()); |
| 164 | |
| 165 | Symbol->setSection(*CurSection); |
| 166 | } |
| 167 | |
| 168 | void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { |
| 169 | switch (Flag) { |
| 170 | case MCAF_SubsectionsViaSymbols: |
| 171 | getAssembler().setSubsectionsViaSymbols(true); |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | assert(0 && "invalid assembler flag!"); |
| 176 | } |
| 177 | |
| 178 | void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { |
| 179 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 180 | // MCObjectStreamer. |
| 181 | // FIXME: Lift context changes into super class. |
| 182 | getAssembler().getOrCreateSymbolData(*Symbol); |
| 183 | Symbol->setVariableValue(AddValueSymbols(Value)); |
| 184 | } |
| 185 | |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 186 | static void SetBinding(MCSymbolData &SD, unsigned Binding) { |
| 187 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 188 | Binding == ELF::STB_WEAK); |
| 189 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift); |
| 190 | SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift)); |
| 191 | } |
| 192 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 193 | static unsigned GetBinding(const MCSymbolData &SD) { |
| 194 | uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift; |
| 195 | assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL || |
| 196 | Binding == ELF::STB_WEAK); |
| 197 | return Binding; |
| 198 | } |
| 199 | |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 200 | static void SetType(MCSymbolData &SD, unsigned Type) { |
| 201 | assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT || |
| 202 | Type == ELF::STT_FUNC || Type == ELF::STT_SECTION || |
| 203 | Type == ELF::STT_FILE || Type == ELF::STT_COMMON || |
| 204 | Type == ELF::STT_TLS); |
| 205 | |
| 206 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift); |
| 207 | SD.setFlags(OtherFlags | (Type << ELF_STT_Shift)); |
| 208 | } |
| 209 | |
| 210 | static void SetVisibility(MCSymbolData &SD, unsigned Visibility) { |
| 211 | assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL || |
| 212 | Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED); |
| 213 | |
| 214 | uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift); |
| 215 | SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift)); |
| 216 | } |
| 217 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 218 | void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, |
| 219 | MCSymbolAttr Attribute) { |
| 220 | // Indirect symbols are handled differently, to match how 'as' handles |
| 221 | // them. This makes writing matching .o files easier. |
| 222 | if (Attribute == MCSA_IndirectSymbol) { |
| 223 | // Note that we intentionally cannot use the symbol data here; this is |
| 224 | // important for matching the string table that 'as' generates. |
| 225 | IndirectSymbolData ISD; |
| 226 | ISD.Symbol = Symbol; |
| 227 | ISD.SectionData = getCurrentSectionData(); |
| 228 | getAssembler().getIndirectSymbols().push_back(ISD); |
| 229 | return; |
| 230 | } |
| 231 | |
| 232 | // Adding a symbol attribute always introduces the symbol, note that an |
| 233 | // important side effect of calling getOrCreateSymbolData here is to register |
| 234 | // the symbol with the assembler. |
| 235 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 236 | |
| 237 | // The implementation of symbol attributes is designed to match 'as', but it |
| 238 | // leaves much to desired. It doesn't really make sense to arbitrarily add and |
| 239 | // remove flags, but 'as' allows this (in particular, see .desc). |
| 240 | // |
| 241 | // In the future it might be worth trying to make these operations more well |
| 242 | // defined. |
| 243 | switch (Attribute) { |
| 244 | case MCSA_LazyReference: |
| 245 | case MCSA_Reference: |
| 246 | case MCSA_NoDeadStrip: |
| 247 | case MCSA_PrivateExtern: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 248 | case MCSA_WeakDefinition: |
Eli Friedman | f8020a3 | 2010-08-16 19:15:06 +0000 | [diff] [blame] | 249 | case MCSA_WeakDefAutoPrivate: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 250 | case MCSA_Invalid: |
| 251 | case MCSA_ELF_TypeIndFunction: |
| 252 | case MCSA_IndirectSymbol: |
| 253 | assert(0 && "Invalid symbol attribute for ELF!"); |
| 254 | break; |
| 255 | |
| 256 | case MCSA_Global: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 257 | SetBinding(SD, ELF::STB_GLOBAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 258 | SD.setExternal(true); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 259 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
Benjamin Kramer | 230c274 | 2010-09-02 17:18:32 +0000 | [diff] [blame] | 262 | case MCSA_WeakReference: |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 263 | case MCSA_Weak: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 264 | SetBinding(SD, ELF::STB_WEAK); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 265 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 266 | break; |
| 267 | |
| 268 | case MCSA_Local: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 269 | SetBinding(SD, ELF::STB_LOCAL); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 270 | SD.setExternal(false); |
| 271 | BindingExplicitlySet.insert(Symbol); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 272 | break; |
| 273 | |
| 274 | case MCSA_ELF_TypeFunction: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 275 | SetType(SD, ELF::STT_FUNC); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 276 | break; |
| 277 | |
| 278 | case MCSA_ELF_TypeObject: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 279 | SetType(SD, ELF::STT_OBJECT); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 280 | break; |
| 281 | |
| 282 | case MCSA_ELF_TypeTLS: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 283 | SetType(SD, ELF::STT_TLS); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 284 | break; |
| 285 | |
| 286 | case MCSA_ELF_TypeCommon: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 287 | SetType(SD, ELF::STT_COMMON); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 288 | break; |
| 289 | |
| 290 | case MCSA_ELF_TypeNoType: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 291 | SetType(SD, ELF::STT_NOTYPE); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 292 | break; |
| 293 | |
| 294 | case MCSA_Protected: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 295 | SetVisibility(SD, ELF::STV_PROTECTED); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 296 | break; |
| 297 | |
| 298 | case MCSA_Hidden: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 299 | SetVisibility(SD, ELF::STV_HIDDEN); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 300 | break; |
| 301 | |
| 302 | case MCSA_Internal: |
Rafael Espindola | f713384 | 2010-09-13 17:39:45 +0000 | [diff] [blame] | 303 | SetVisibility(SD, ELF::STV_INTERNAL); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 304 | break; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 309 | unsigned ByteAlignment) { |
| 310 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 311 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 312 | if (!BindingExplicitlySet.count(Symbol)) { |
| 313 | SetBinding(SD, ELF::STB_GLOBAL); |
| 314 | SD.setExternal(true); |
| 315 | } |
| 316 | |
| 317 | if (GetBinding(SD) == ELF_STB_Local) { |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 318 | const MCSection *Section = getAssembler().getContext().getELFSection(".bss", |
| 319 | MCSectionELF::SHT_NOBITS, |
| 320 | MCSectionELF::SHF_WRITE | |
| 321 | MCSectionELF::SHF_ALLOC, |
| 322 | SectionKind::getBSS()); |
| 323 | |
| 324 | MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section); |
Rafael Espindola | 1963572 | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 325 | new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); |
| 326 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 327 | MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); |
| 328 | SD.setFragment(F); |
| 329 | Symbol->setSection(*Section); |
Rafael Espindola | 1963572 | 2010-09-22 17:43:04 +0000 | [diff] [blame] | 330 | |
| 331 | // Update the maximum alignment of the section if necessary. |
| 332 | if (ByteAlignment > SectData.getAlignment()) |
| 333 | SectData.setAlignment(ByteAlignment); |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 334 | } else { |
| 335 | SD.setCommon(Size, ByteAlignment); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Rafael Espindola | f7c10a3 | 2010-09-21 00:24:38 +0000 | [diff] [blame] | 338 | SD.setSize(MCConstantExpr::Create(Size, getContext())); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { |
| 342 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 343 | // MCObjectStreamer. |
| 344 | getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); |
| 345 | } |
| 346 | |
| 347 | void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size, |
| 348 | unsigned AddrSpace) { |
| 349 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 350 | // MCObjectStreamer. |
| 351 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 352 | |
| 353 | // Avoid fixups when possible. |
| 354 | int64_t AbsValue; |
| 355 | if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) { |
| 356 | // FIXME: Endianness assumption. |
| 357 | for (unsigned i = 0; i != Size; ++i) |
| 358 | DF->getContents().push_back(uint8_t(AbsValue >> (i * 8))); |
| 359 | } else { |
| 360 | DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value), |
| 361 | MCFixup::getKindForSize(Size))); |
| 362 | DF->getContents().resize(DF->getContents().size() + Size, 0); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, |
| 367 | int64_t Value, unsigned ValueSize, |
| 368 | unsigned MaxBytesToEmit) { |
| 369 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 370 | // MCObjectStreamer. |
| 371 | if (MaxBytesToEmit == 0) |
| 372 | MaxBytesToEmit = ByteAlignment; |
| 373 | new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, |
| 374 | getCurrentSectionData()); |
| 375 | |
| 376 | // Update the maximum alignment on the current section if necessary. |
| 377 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 378 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 379 | } |
| 380 | |
| 381 | void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment, |
| 382 | unsigned MaxBytesToEmit) { |
| 383 | // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into |
| 384 | // MCObjectStreamer. |
| 385 | if (MaxBytesToEmit == 0) |
| 386 | MaxBytesToEmit = ByteAlignment; |
| 387 | MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, |
| 388 | getCurrentSectionData()); |
| 389 | F->setEmitNops(true); |
| 390 | |
| 391 | // Update the maximum alignment on the current section if necessary. |
| 392 | if (ByteAlignment > getCurrentSectionData()->getAlignment()) |
| 393 | getCurrentSectionData()->setAlignment(ByteAlignment); |
| 394 | } |
| 395 | |
| 396 | void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset, |
| 397 | unsigned char Value) { |
| 398 | // TODO: This is exactly the same as MCMachOStreamer. Consider merging into |
| 399 | // MCObjectStreamer. |
| 400 | new MCOrgFragment(*Offset, Value, getCurrentSectionData()); |
| 401 | } |
| 402 | |
| 403 | // Add a symbol for the file name of this module. This is the second |
| 404 | // entry in the module's symbol table (the first being the null symbol). |
| 405 | void MCELFStreamer::EmitFileDirective(StringRef Filename) { |
| 406 | MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename); |
| 407 | Symbol->setSection(*CurSection); |
| 408 | Symbol->setAbsolute(); |
| 409 | |
| 410 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 411 | |
| 412 | SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default); |
| 413 | } |
| 414 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 415 | void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { |
| 416 | MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData()); |
| 417 | |
| 418 | // Add the fixups and data. |
| 419 | // |
| 420 | // FIXME: Revisit this design decision when relaxation is done, we may be |
| 421 | // able to get away with not storing any extra data in the MCInst. |
| 422 | SmallVector<MCFixup, 4> Fixups; |
| 423 | SmallString<256> Code; |
| 424 | raw_svector_ostream VecOS(Code); |
| 425 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 426 | VecOS.flush(); |
| 427 | |
| 428 | IF->getCode() = Code; |
| 429 | IF->getFixups() = Fixups; |
| 430 | } |
| 431 | |
| 432 | void MCELFStreamer::EmitInstToData(const MCInst &Inst) { |
| 433 | MCDataFragment *DF = getOrCreateDataFragment(); |
| 434 | |
| 435 | SmallVector<MCFixup, 4> Fixups; |
| 436 | SmallString<256> Code; |
| 437 | raw_svector_ostream VecOS(Code); |
| 438 | getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); |
| 439 | VecOS.flush(); |
| 440 | |
| 441 | // Add the fixups and data. |
| 442 | for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { |
| 443 | Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); |
| 444 | DF->addFixup(Fixups[i]); |
| 445 | } |
| 446 | DF->getContents().append(Code.begin(), Code.end()); |
| 447 | } |
| 448 | |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 449 | void MCELFStreamer::EmitInstruction(const MCInst &Inst) { |
| 450 | // Scan for values. |
| 451 | for (unsigned i = 0; i != Inst.getNumOperands(); ++i) |
| 452 | if (Inst.getOperand(i).isExpr()) |
| 453 | AddValueSymbols(Inst.getOperand(i).getExpr()); |
| 454 | |
| 455 | getCurrentSectionData()->setHasInstructions(true); |
| 456 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 457 | // If this instruction doesn't need relaxation, just emit it as data. |
| 458 | if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) { |
| 459 | EmitInstToData(Inst); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 463 | // Otherwise, if we are relaxing everything, relax the instruction as much as |
| 464 | // possible and emit it as data. |
| 465 | if (getAssembler().getRelaxAll()) { |
| 466 | MCInst Relaxed; |
| 467 | getAssembler().getBackend().RelaxInstruction(Inst, Relaxed); |
| 468 | while (getAssembler().getBackend().MayNeedRelaxation(Relaxed)) |
| 469 | getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed); |
| 470 | EmitInstToData(Relaxed); |
| 471 | return; |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 472 | } |
Benjamin Kramer | 93ded73 | 2010-08-27 10:40:51 +0000 | [diff] [blame] | 473 | |
| 474 | // Otherwise emit to a separate fragment. |
| 475 | EmitInstToFragment(Inst); |
Matt Fleming | 3565a06 | 2010-08-16 18:57:57 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | void MCELFStreamer::Finish() { |
| 479 | getAssembler().Finish(); |
| 480 | } |
| 481 | |
| 482 | MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 483 | raw_ostream &OS, MCCodeEmitter *CE, |
| 484 | bool RelaxAll) { |
| 485 | MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE); |
| 486 | if (RelaxAll) |
| 487 | S->getAssembler().setRelaxAll(true); |
| 488 | return S; |
| 489 | } |