MCELFStreamer.cpp revision 226633
174462Salfred//===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===// 274462Salfred// 3261046Smav// The LLVM Compiler Infrastructure 4261046Smav// 5261046Smav// This file is distributed under the University of Illinois Open Source 68870Srgrimes// License. See LICENSE.TXT for details. 7261046Smav// 8261046Smav//===----------------------------------------------------------------------===// 9261046Smav// 10261046Smav// This file assembles .s files and emits ELF .o object files. 11261046Smav// 12261046Smav//===----------------------------------------------------------------------===// 13261046Smav 14261046Smav#include "MCELFStreamer.h" 15261046Smav#include "MCELF.h" 16261046Smav#include "llvm/MC/MCStreamer.h" 17261046Smav#include "llvm/MC/MCCodeEmitter.h" 18261046Smav#include "llvm/MC/MCELFSymbolFlags.h" 19261046Smav#include "llvm/MC/MCExpr.h" 20261046Smav#include "llvm/MC/MCInst.h" 21261046Smav#include "llvm/MC/MCSection.h" 22261046Smav#include "llvm/MC/MCSymbol.h" 23261046Smav#include "llvm/MC/MCValue.h" 24261046Smav#include "llvm/MC/MCAsmBackend.h" 25261046Smav#include "llvm/Support/Debug.h" 26261046Smav#include "llvm/Support/ELF.h" 27261046Smav#include "llvm/Support/ErrorHandling.h" 28261046Smav#include "llvm/Support/raw_ostream.h" 291901Swollman 3074462Salfredusing namespace llvm; 3174462Salfred 3274462Salfredvoid MCELFStreamer::InitSections() { 331901Swollman // This emulates the same behavior of GNU as. This makes it easier 341901Swollman // to compare the output as the major sections are in the same order. 35136581Sobrien SetSectionText(); 3692990Sobrien SetSectionData(); 371901Swollman SetSectionBss(); 3892990Sobrien SetSectionText(); 3992990Sobrien} 401901Swollman 418870Srgrimesvoid MCELFStreamer::EmitLabel(MCSymbol *Symbol) { 421901Swollman assert(Symbol->isUndefined() && "Cannot define a symbol twice!"); 4374462Salfred 441901Swollman MCObjectStreamer::EmitLabel(Symbol); 451901Swollman 461901Swollman const MCSectionELF &Section = 4775094Siedowse static_cast<const MCSectionELF&>(Symbol->getSection()); 4874462Salfred MCSymbolData &SD = getAssembler().getSymbolData(*Symbol); 4921070Speter if (Section.getFlags() & ELF::SHF_TLS) 501901Swollman MCELF::SetType(SD, ELF::STT_TLS); 5174462Salfred} 5274462Salfred 5374462Salfredvoid MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) { 541901Swollman switch (Flag) { 5574462Salfred case MCAF_SyntaxUnified: return; // no-op here. 5611666Sphk case MCAF_Code16: return; // Change parsing mode; no-op here. 5771579Sdeischen case MCAF_Code32: return; // Change parsing mode; no-op here. 58156090Sdeischen case MCAF_Code64: return; // Change parsing mode; no-op here. 591901Swollman case MCAF_SubsectionsViaSymbols: 6074462Salfred getAssembler().setSubsectionsViaSymbols(true); 6174462Salfred return; 6274462Salfred } 631901Swollman 6474462Salfred assert(0 && "invalid assembler flag!"); 6574462Salfred} 6674462Salfred 6774462Salfredvoid MCELFStreamer::EmitThumbFunc(MCSymbol *Func) { 6874462Salfred // FIXME: Anything needed here to flag the function as thumb? 6974462Salfred 7074462Salfred getAssembler().setIsThumbFunc(Func); 7174462Salfred 7274462Salfred MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func); 7374462Salfred SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc); 7474462Salfred} 7574462Salfred 7674462Salfredvoid MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) { 7774462Salfred // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 78204950Sjhb // MCObjectStreamer. 79204950Sjhb // FIXME: Lift context changes into super class. 80204950Sjhb getAssembler().getOrCreateSymbolData(*Symbol); 8174462Salfred Symbol->setVariableValue(AddValueSymbols(Value)); 82204950Sjhb} 8392905Sobrien 8474462Salfredvoid MCELFStreamer::ChangeSection(const MCSection *Section) { 8574462Salfred const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup(); 8674462Salfred if (Grp) 871901Swollman getAssembler().getOrCreateSymbolData(*Grp); 8874462Salfred this->MCObjectStreamer::ChangeSection(Section); 8974462Salfred} 9074462Salfred 9174462Salfredvoid MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) { 9274462Salfred getAssembler().getOrCreateSymbolData(*Symbol); 9374462Salfred MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias); 9474462Salfred AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref); 9574462Salfred const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext()); 9674462Salfred Alias->setVariableValue(Value); 97204950Sjhb} 98204950Sjhb 99204950Sjhbvoid MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol, 100204950Sjhb MCSymbolAttr Attribute) { 101204950Sjhb // Indirect symbols are handled differently, to match how 'as' handles 102204950Sjhb // them. This makes writing matching .o files easier. 103204950Sjhb if (Attribute == MCSA_IndirectSymbol) { 10474462Salfred // Note that we intentionally cannot use the symbol data here; this is 10574462Salfred // important for matching the string table that 'as' generates. 10674462Salfred IndirectSymbolData ISD; 10774462Salfred ISD.Symbol = Symbol; 10874462Salfred ISD.SectionData = getCurrentSectionData(); 10974462Salfred getAssembler().getIndirectSymbols().push_back(ISD); 11074462Salfred return; 11174462Salfred } 11274462Salfred 11374462Salfred // Adding a symbol attribute always introduces the symbol, note that an 11474462Salfred // important side effect of calling getOrCreateSymbolData here is to register 11574462Salfred // the symbol with the assembler. 11674462Salfred MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 11774462Salfred 11874462Salfred // The implementation of symbol attributes is designed to match 'as', but it 11974462Salfred // leaves much to desired. It doesn't really make sense to arbitrarily add and 12074462Salfred // remove flags, but 'as' allows this (in particular, see .desc). 12174462Salfred // 12274462Salfred // In the future it might be worth trying to make these operations more well 1231901Swollman // defined. 1241901Swollman switch (Attribute) { 12574462Salfred case MCSA_LazyReference: 1261901Swollman case MCSA_Reference: 12774462Salfred case MCSA_NoDeadStrip: 12874462Salfred case MCSA_SymbolResolver: 12974462Salfred case MCSA_PrivateExtern: 130204950Sjhb case MCSA_WeakDefinition: 131204950Sjhb case MCSA_WeakDefAutoPrivate: 132204950Sjhb case MCSA_Invalid: 133204950Sjhb case MCSA_ELF_TypeIndFunction: 134204950Sjhb case MCSA_IndirectSymbol: 13574462Salfred assert(0 && "Invalid symbol attribute for ELF!"); 13674462Salfred break; 1371901Swollman 13874462Salfred case MCSA_ELF_TypeGnuUniqueObject: 13974462Salfred // Ignore for now. 14074462Salfred break; 14174462Salfred 14274462Salfred case MCSA_Global: 14374462Salfred MCELF::SetBinding(SD, ELF::STB_GLOBAL); 14474462Salfred SD.setExternal(true); 14574462Salfred BindingExplicitlySet.insert(Symbol); 14674462Salfred break; 14774462Salfred 14874462Salfred case MCSA_WeakReference: 14974462Salfred case MCSA_Weak: 15074462Salfred MCELF::SetBinding(SD, ELF::STB_WEAK); 1511901Swollman SD.setExternal(true); 152121651Smbr BindingExplicitlySet.insert(Symbol); 15374462Salfred break; 15474462Salfred 15574462Salfred case MCSA_Local: 15674462Salfred MCELF::SetBinding(SD, ELF::STB_LOCAL); 15774462Salfred SD.setExternal(false); 15874462Salfred BindingExplicitlySet.insert(Symbol); 15974462Salfred break; 16074462Salfred 16174462Salfred case MCSA_ELF_TypeFunction: 16274462Salfred MCELF::SetType(SD, ELF::STT_FUNC); 16374462Salfred break; 16474462Salfred 16574462Salfred case MCSA_ELF_TypeObject: 16674462Salfred MCELF::SetType(SD, ELF::STT_OBJECT); 16774462Salfred break; 16874462Salfred 16974462Salfred case MCSA_ELF_TypeTLS: 17074462Salfred MCELF::SetType(SD, ELF::STT_TLS); 1711901Swollman break; 17274462Salfred 17374462Salfred case MCSA_ELF_TypeCommon: 17474462Salfred MCELF::SetType(SD, ELF::STT_COMMON); 17574462Salfred break; 17674462Salfred 1771901Swollman case MCSA_ELF_TypeNoType: 1781901Swollman MCELF::SetType(SD, ELF::STT_NOTYPE); 17974462Salfred break; 18074462Salfred 18174462Salfred case MCSA_Protected: 18274462Salfred MCELF::SetVisibility(SD, ELF::STV_PROTECTED); 18374462Salfred break; 18474462Salfred 18574462Salfred case MCSA_Hidden: 18674462Salfred MCELF::SetVisibility(SD, ELF::STV_HIDDEN); 18774462Salfred break; 18874462Salfred 18974462Salfred case MCSA_Internal: 19074462Salfred MCELF::SetVisibility(SD, ELF::STV_INTERNAL); 19174462Salfred break; 19274462Salfred } 19374462Salfred} 1941901Swollman 1951901Swollmanvoid MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size, 19674462Salfred unsigned ByteAlignment) { 19774462Salfred MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 1981901Swollman 1998870Srgrimes if (!BindingExplicitlySet.count(Symbol)) { 2001901Swollman MCELF::SetBinding(SD, ELF::STB_GLOBAL); 2011901Swollman SD.setExternal(true); 2021901Swollman } 20374462Salfred 20474462Salfred MCELF::SetType(SD, ELF::STT_OBJECT); 2051901Swollman 206 if (MCELF::GetBinding(SD) == ELF_STB_Local) { 207 const MCSection *Section = getAssembler().getContext().getELFSection(".bss", 208 ELF::SHT_NOBITS, 209 ELF::SHF_WRITE | 210 ELF::SHF_ALLOC, 211 SectionKind::getBSS()); 212 Symbol->setSection(*Section); 213 214 struct LocalCommon L = {&SD, Size, ByteAlignment}; 215 LocalCommons.push_back(L); 216 } else { 217 SD.setCommon(Size, ByteAlignment); 218 } 219 220 SD.setSize(MCConstantExpr::Create(Size, getContext())); 221} 222 223void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size, 224 unsigned ByteAlignment) { 225 // FIXME: Should this be caught and done earlier? 226 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 227 MCELF::SetBinding(SD, ELF::STB_LOCAL); 228 SD.setExternal(false); 229 BindingExplicitlySet.insert(Symbol); 230 EmitCommonSymbol(Symbol, Size, ByteAlignment); 231} 232 233void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) { 234 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 235 // MCObjectStreamer. 236 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end()); 237} 238 239void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment, 240 int64_t Value, unsigned ValueSize, 241 unsigned MaxBytesToEmit) { 242 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 243 // MCObjectStreamer. 244 if (MaxBytesToEmit == 0) 245 MaxBytesToEmit = ByteAlignment; 246 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit, 247 getCurrentSectionData()); 248 249 // Update the maximum alignment on the current section if necessary. 250 if (ByteAlignment > getCurrentSectionData()->getAlignment()) 251 getCurrentSectionData()->setAlignment(ByteAlignment); 252} 253 254void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment, 255 unsigned MaxBytesToEmit) { 256 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into 257 // MCObjectStreamer. 258 if (MaxBytesToEmit == 0) 259 MaxBytesToEmit = ByteAlignment; 260 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit, 261 getCurrentSectionData()); 262 F->setEmitNops(true); 263 264 // Update the maximum alignment on the current section if necessary. 265 if (ByteAlignment > getCurrentSectionData()->getAlignment()) 266 getCurrentSectionData()->setAlignment(ByteAlignment); 267} 268 269// Add a symbol for the file name of this module. This is the second 270// entry in the module's symbol table (the first being the null symbol). 271void MCELFStreamer::EmitFileDirective(StringRef Filename) { 272 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename); 273 Symbol->setSection(*getCurrentSection()); 274 Symbol->setAbsolute(); 275 276 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol); 277 278 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default); 279} 280 281void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) { 282 switch (expr->getKind()) { 283 case MCExpr::Target: llvm_unreachable("Can't handle target exprs yet!"); 284 case MCExpr::Constant: 285 break; 286 287 case MCExpr::Binary: { 288 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr); 289 fixSymbolsInTLSFixups(be->getLHS()); 290 fixSymbolsInTLSFixups(be->getRHS()); 291 break; 292 } 293 294 case MCExpr::SymbolRef: { 295 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr); 296 switch (symRef.getKind()) { 297 default: 298 return; 299 case MCSymbolRefExpr::VK_GOTTPOFF: 300 case MCSymbolRefExpr::VK_INDNTPOFF: 301 case MCSymbolRefExpr::VK_NTPOFF: 302 case MCSymbolRefExpr::VK_GOTNTPOFF: 303 case MCSymbolRefExpr::VK_TLSGD: 304 case MCSymbolRefExpr::VK_TLSLD: 305 case MCSymbolRefExpr::VK_TLSLDM: 306 case MCSymbolRefExpr::VK_TPOFF: 307 case MCSymbolRefExpr::VK_DTPOFF: 308 case MCSymbolRefExpr::VK_ARM_TLSGD: 309 case MCSymbolRefExpr::VK_ARM_TPOFF: 310 case MCSymbolRefExpr::VK_ARM_GOTTPOFF: 311 break; 312 } 313 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol()); 314 MCELF::SetType(SD, ELF::STT_TLS); 315 break; 316 } 317 318 case MCExpr::Unary: 319 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr()); 320 break; 321 } 322} 323 324void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) { 325 this->MCObjectStreamer::EmitInstToFragment(Inst); 326 MCInstFragment &F = *cast<MCInstFragment>(getCurrentFragment()); 327 328 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i) 329 fixSymbolsInTLSFixups(F.getFixups()[i].getValue()); 330} 331 332void MCELFStreamer::EmitInstToData(const MCInst &Inst) { 333 MCDataFragment *DF = getOrCreateDataFragment(); 334 335 SmallVector<MCFixup, 4> Fixups; 336 SmallString<256> Code; 337 raw_svector_ostream VecOS(Code); 338 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups); 339 VecOS.flush(); 340 341 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) 342 fixSymbolsInTLSFixups(Fixups[i].getValue()); 343 344 // Add the fixups and data. 345 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) { 346 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size()); 347 DF->addFixup(Fixups[i]); 348 } 349 DF->getContents().append(Code.begin(), Code.end()); 350} 351 352void MCELFStreamer::Finish() { 353 EmitFrames(true); 354 355 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(), 356 e = LocalCommons.end(); 357 i != e; ++i) { 358 MCSymbolData *SD = i->SD; 359 uint64_t Size = i->Size; 360 unsigned ByteAlignment = i->ByteAlignment; 361 const MCSymbol &Symbol = SD->getSymbol(); 362 const MCSection &Section = Symbol.getSection(); 363 364 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section); 365 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData); 366 367 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData); 368 SD->setFragment(F); 369 370 // Update the maximum alignment of the section if necessary. 371 if (ByteAlignment > SectData.getAlignment()) 372 SectData.setAlignment(ByteAlignment); 373 } 374 375 this->MCObjectStreamer::Finish(); 376} 377 378MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB, 379 raw_ostream &OS, MCCodeEmitter *CE, 380 bool RelaxAll, bool NoExecStack) { 381 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE); 382 if (RelaxAll) 383 S->getAssembler().setRelaxAll(true); 384 if (NoExecStack) 385 S->getAssembler().setNoExecStack(true); 386 return S; 387} 388