Jan Sjödin | 24b17c6 | 2011-03-03 14:52:12 +0000 | [diff] [blame] | 1 | //===- lib/MC/MCELFStreamer.h - 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 | #ifndef LLVM_MC_MCELFSTREAMER_H |
| 15 | #define LLVM_MC_MCELFSTREAMER_H |
| 16 | |
| 17 | #include "llvm/ADT/SmallPtrSet.h" |
| 18 | #include "llvm/ADT/DenseMap.h" |
| 19 | #include "llvm/MC/MCAssembler.h" |
| 20 | #include "llvm/MC/MCContext.h" |
| 21 | #include "llvm/MC/MCObjectStreamer.h" |
| 22 | #include "llvm/MC/MCSectionELF.h" |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class MCELFStreamer : public MCObjectStreamer { |
| 27 | public: |
| 28 | MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 29 | raw_ostream &OS, MCCodeEmitter *Emitter) |
| 30 | : MCObjectStreamer(Context, TAB, OS, Emitter) {} |
| 31 | |
Jan Sjödin | 01dff96 | 2011-03-09 17:33:05 +0000 | [diff] [blame^] | 32 | MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 33 | raw_ostream &OS, MCCodeEmitter *Emitter, |
| 34 | MCAssembler *Assembler) |
| 35 | : MCObjectStreamer(Context, TAB, OS, Emitter, Assembler) {} |
| 36 | |
| 37 | |
Jan Sjödin | 24b17c6 | 2011-03-03 14:52:12 +0000 | [diff] [blame] | 38 | ~MCELFStreamer() {} |
| 39 | |
| 40 | /// @name MCStreamer Interface |
| 41 | /// @{ |
| 42 | |
| 43 | virtual void InitSections(); |
| 44 | virtual void ChangeSection(const MCSection *Section); |
| 45 | virtual void EmitLabel(MCSymbol *Symbol); |
| 46 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
| 47 | virtual void EmitThumbFunc(MCSymbol *Func); |
| 48 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
| 49 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); |
| 50 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
| 51 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 52 | assert(0 && "ELF doesn't support this directive"); |
| 53 | } |
| 54 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 55 | unsigned ByteAlignment); |
| 56 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 57 | assert(0 && "ELF doesn't support this directive"); |
| 58 | } |
| 59 | |
| 60 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 61 | assert(0 && "ELF doesn't support this directive"); |
| 62 | } |
| 63 | |
| 64 | virtual void EmitCOFFSymbolType(int Type) { |
| 65 | assert(0 && "ELF doesn't support this directive"); |
| 66 | } |
| 67 | |
| 68 | virtual void EndCOFFSymbolDef() { |
| 69 | assert(0 && "ELF doesn't support this directive"); |
| 70 | } |
| 71 | |
| 72 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 73 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 74 | SD.setSize(Value); |
| 75 | } |
| 76 | |
| 77 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size); |
| 78 | |
| 79 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
| 80 | unsigned Size = 0, unsigned ByteAlignment = 0) { |
| 81 | assert(0 && "ELF doesn't support this directive"); |
| 82 | } |
| 83 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 84 | uint64_t Size, unsigned ByteAlignment = 0) { |
| 85 | assert(0 && "ELF doesn't support this directive"); |
| 86 | } |
| 87 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
| 88 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 89 | unsigned ValueSize = 1, |
| 90 | unsigned MaxBytesToEmit = 0); |
| 91 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 92 | unsigned MaxBytesToEmit = 0); |
| 93 | |
| 94 | virtual void EmitFileDirective(StringRef Filename); |
| 95 | |
| 96 | virtual void Finish(); |
| 97 | |
| 98 | private: |
| 99 | virtual void EmitInstToFragment(const MCInst &Inst); |
| 100 | virtual void EmitInstToData(const MCInst &Inst); |
| 101 | |
| 102 | void fixSymbolsInTLSFixups(const MCExpr *expr); |
| 103 | |
| 104 | struct LocalCommon { |
| 105 | MCSymbolData *SD; |
| 106 | uint64_t Size; |
| 107 | unsigned ByteAlignment; |
| 108 | }; |
| 109 | std::vector<LocalCommon> LocalCommons; |
| 110 | |
| 111 | SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet; |
| 112 | /// @} |
| 113 | void SetSection(StringRef Section, unsigned Type, unsigned Flags, |
| 114 | SectionKind Kind) { |
| 115 | SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); |
| 116 | } |
| 117 | |
| 118 | void SetSectionData() { |
| 119 | SetSection(".data", ELF::SHT_PROGBITS, |
| 120 | ELF::SHF_WRITE |ELF::SHF_ALLOC, |
| 121 | SectionKind::getDataRel()); |
| 122 | EmitCodeAlignment(4, 0); |
| 123 | } |
| 124 | void SetSectionText() { |
| 125 | SetSection(".text", ELF::SHT_PROGBITS, |
| 126 | ELF::SHF_EXECINSTR | |
| 127 | ELF::SHF_ALLOC, SectionKind::getText()); |
| 128 | EmitCodeAlignment(4, 0); |
| 129 | } |
| 130 | void SetSectionBss() { |
| 131 | SetSection(".bss", ELF::SHT_NOBITS, |
| 132 | ELF::SHF_WRITE | |
| 133 | ELF::SHF_ALLOC, SectionKind::getBSS()); |
| 134 | EmitCodeAlignment(4, 0); |
| 135 | } |
| 136 | }; |
| 137 | |
| 138 | } // end llvm namespace |
| 139 | |
| 140 | #endif |
| 141 | //===- lib/MC/MCELFStreamer.h - ELF Object Output -------------------------===// |
| 142 | // |
| 143 | // The LLVM Compiler Infrastructure |
| 144 | // |
| 145 | // This file is distributed under the University of Illinois Open Source |
| 146 | // License. See LICENSE.TXT for details. |
| 147 | // |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | // |
| 150 | // This file assembles .s files and emits ELF .o object files. |
| 151 | // |
| 152 | //===----------------------------------------------------------------------===// |
| 153 | |
| 154 | #ifndef LLVM_MC_MCELFSTREAMER_H |
| 155 | #define LLVM_MC_MCELFSTREAMER_H |
| 156 | |
| 157 | #include "llvm/ADT/SmallPtrSet.h" |
| 158 | #include "llvm/ADT/DenseMap.h" |
| 159 | #include "llvm/MC/MCAssembler.h" |
| 160 | #include "llvm/MC/MCContext.h" |
| 161 | #include "llvm/MC/MCObjectStreamer.h" |
| 162 | #include "llvm/MC/MCSectionELF.h" |
| 163 | |
| 164 | namespace llvm { |
| 165 | |
| 166 | class MCELFStreamer : public MCObjectStreamer { |
| 167 | public: |
| 168 | MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB, |
| 169 | raw_ostream &OS, MCCodeEmitter *Emitter) |
| 170 | : MCObjectStreamer(Context, TAB, OS, Emitter) {} |
| 171 | |
| 172 | ~MCELFStreamer() {} |
| 173 | |
| 174 | /// @name MCStreamer Interface |
| 175 | /// @{ |
| 176 | |
| 177 | virtual void InitSections(); |
| 178 | virtual void ChangeSection(const MCSection *Section); |
| 179 | virtual void EmitLabel(MCSymbol *Symbol); |
| 180 | virtual void EmitAssemblerFlag(MCAssemblerFlag Flag); |
| 181 | virtual void EmitThumbFunc(MCSymbol *Func); |
| 182 | virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value); |
| 183 | virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol); |
| 184 | virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute); |
| 185 | virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) { |
| 186 | assert(0 && "ELF doesn't support this directive"); |
| 187 | } |
| 188 | virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, |
| 189 | unsigned ByteAlignment); |
| 190 | virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) { |
| 191 | assert(0 && "ELF doesn't support this directive"); |
| 192 | } |
| 193 | |
| 194 | virtual void EmitCOFFSymbolStorageClass(int StorageClass) { |
| 195 | assert(0 && "ELF doesn't support this directive"); |
| 196 | } |
| 197 | |
| 198 | virtual void EmitCOFFSymbolType(int Type) { |
| 199 | assert(0 && "ELF doesn't support this directive"); |
| 200 | } |
| 201 | |
| 202 | virtual void EndCOFFSymbolDef() { |
| 203 | assert(0 && "ELF doesn't support this directive"); |
| 204 | } |
| 205 | |
| 206 | virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) { |
| 207 | MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); |
| 208 | SD.setSize(Value); |
| 209 | } |
| 210 | |
| 211 | virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size); |
| 212 | |
| 213 | virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0, |
| 214 | unsigned Size = 0, unsigned ByteAlignment = 0) { |
| 215 | assert(0 && "ELF doesn't support this directive"); |
| 216 | } |
| 217 | virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol, |
| 218 | uint64_t Size, unsigned ByteAlignment = 0) { |
| 219 | assert(0 && "ELF doesn't support this directive"); |
| 220 | } |
| 221 | virtual void EmitBytes(StringRef Data, unsigned AddrSpace); |
| 222 | virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0, |
| 223 | unsigned ValueSize = 1, |
| 224 | unsigned MaxBytesToEmit = 0); |
| 225 | virtual void EmitCodeAlignment(unsigned ByteAlignment, |
| 226 | unsigned MaxBytesToEmit = 0); |
| 227 | |
| 228 | virtual void EmitFileDirective(StringRef Filename); |
| 229 | |
| 230 | virtual void Finish(); |
| 231 | |
| 232 | private: |
| 233 | virtual void EmitInstToFragment(const MCInst &Inst); |
| 234 | virtual void EmitInstToData(const MCInst &Inst); |
| 235 | |
| 236 | void fixSymbolsInTLSFixups(const MCExpr *expr); |
| 237 | |
| 238 | struct LocalCommon { |
| 239 | MCSymbolData *SD; |
| 240 | uint64_t Size; |
| 241 | unsigned ByteAlignment; |
| 242 | }; |
| 243 | std::vector<LocalCommon> LocalCommons; |
| 244 | |
| 245 | SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet; |
| 246 | /// @} |
| 247 | void SetSection(StringRef Section, unsigned Type, unsigned Flags, |
| 248 | SectionKind Kind) { |
| 249 | SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind)); |
| 250 | } |
| 251 | |
| 252 | void SetSectionData() { |
| 253 | SetSection(".data", ELF::SHT_PROGBITS, |
| 254 | ELF::SHF_WRITE |ELF::SHF_ALLOC, |
| 255 | SectionKind::getDataRel()); |
| 256 | EmitCodeAlignment(4, 0); |
| 257 | } |
| 258 | void SetSectionText() { |
| 259 | SetSection(".text", ELF::SHT_PROGBITS, |
| 260 | ELF::SHF_EXECINSTR | |
| 261 | ELF::SHF_ALLOC, SectionKind::getText()); |
| 262 | EmitCodeAlignment(4, 0); |
| 263 | } |
| 264 | void SetSectionBss() { |
| 265 | SetSection(".bss", ELF::SHT_NOBITS, |
| 266 | ELF::SHF_WRITE | |
| 267 | ELF::SHF_ALLOC, SectionKind::getBSS()); |
| 268 | EmitCodeAlignment(4, 0); |
| 269 | } |
| 270 | }; |
| 271 | |
| 272 | } // end llvm namespace |
| 273 | |
| 274 | #endif |