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