comparison lib/MC/MCContext.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children 3a76565eade5
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "llvm/MC/MCContext.h" 10 #include "llvm/MC/MCContext.h"
11 #include "llvm/ADT/SmallString.h" 11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringMap.h"
14 #include "llvm/ADT/StringRef.h"
12 #include "llvm/ADT/Twine.h" 15 #include "llvm/ADT/Twine.h"
16 #include "llvm/BinaryFormat/COFF.h"
17 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCAsmInfo.h" 18 #include "llvm/MC/MCAsmInfo.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeView.h" 19 #include "llvm/MC/MCCodeView.h"
16 #include "llvm/MC/MCDwarf.h" 20 #include "llvm/MC/MCDwarf.h"
21 #include "llvm/MC/MCExpr.h"
22 #include "llvm/MC/MCFragment.h"
17 #include "llvm/MC/MCLabel.h" 23 #include "llvm/MC/MCLabel.h"
18 #include "llvm/MC/MCObjectFileInfo.h" 24 #include "llvm/MC/MCObjectFileInfo.h"
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/MC/MCSectionCOFF.h" 25 #include "llvm/MC/MCSectionCOFF.h"
21 #include "llvm/MC/MCSectionELF.h" 26 #include "llvm/MC/MCSectionELF.h"
22 #include "llvm/MC/MCSectionMachO.h" 27 #include "llvm/MC/MCSectionMachO.h"
28 #include "llvm/MC/MCSectionWasm.h"
23 #include "llvm/MC/MCStreamer.h" 29 #include "llvm/MC/MCStreamer.h"
30 #include "llvm/MC/MCSymbol.h"
24 #include "llvm/MC/MCSymbolCOFF.h" 31 #include "llvm/MC/MCSymbolCOFF.h"
25 #include "llvm/MC/MCSymbolELF.h" 32 #include "llvm/MC/MCSymbolELF.h"
26 #include "llvm/MC/MCSymbolMachO.h" 33 #include "llvm/MC/MCSymbolMachO.h"
27 #include "llvm/Support/COFF.h" 34 #include "llvm/MC/MCSymbolWasm.h"
35 #include "llvm/MC/SectionKind.h"
36 #include "llvm/Support/Casting.h"
28 #include "llvm/Support/CommandLine.h" 37 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/ELF.h"
30 #include "llvm/Support/ErrorHandling.h" 38 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/MemoryBuffer.h" 39 #include "llvm/Support/MemoryBuffer.h"
32 #include "llvm/Support/Signals.h" 40 #include "llvm/Support/Signals.h"
33 #include "llvm/Support/SourceMgr.h" 41 #include "llvm/Support/SourceMgr.h"
42 #include "llvm/Support/raw_ostream.h"
43 #include <cassert>
44 #include <cstdlib>
45 #include <tuple>
46 #include <utility>
34 47
35 using namespace llvm; 48 using namespace llvm;
36 49
37 static cl::opt<char*> 50 static cl::opt<char*>
38 AsSecureLogFileName("as-secure-log-file-name", 51 AsSecureLogFileName("as-secure-log-file-name",
39 cl::desc("As secure log file name (initialized from " 52 cl::desc("As secure log file name (initialized from "
40 "AS_SECURE_LOG_FILE env variable)"), 53 "AS_SECURE_LOG_FILE env variable)"),
41 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden); 54 cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
42 55
43
44 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, 56 MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
45 const MCObjectFileInfo *mofi, const SourceMgr *mgr, 57 const MCObjectFileInfo *mofi, const SourceMgr *mgr,
46 bool DoAutoReset) 58 bool DoAutoReset)
47 : SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(), 59 : SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
48 Symbols(Allocator), UsedNames(Allocator), 60 Symbols(Allocator), UsedNames(Allocator),
49 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false), 61 CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
50 GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4), 62 AutoReset(DoAutoReset) {
51 AllowTemporaryLabels(true), DwarfCompileUnitID(0),
52 AutoReset(DoAutoReset), HadError(false) {
53 SecureLogFile = AsSecureLogFileName; 63 SecureLogFile = AsSecureLogFileName;
54 SecureLog = nullptr;
55 SecureLogUsed = false;
56 64
57 if (SrcMgr && SrcMgr->getNumBuffers()) 65 if (SrcMgr && SrcMgr->getNumBuffers())
58 MainFileName = 66 MainFileName =
59 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier(); 67 SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
60 } 68 }
78 MachOAllocator.DestroyAll(); 86 MachOAllocator.DestroyAll();
79 87
80 MCSubtargetAllocator.DestroyAll(); 88 MCSubtargetAllocator.DestroyAll();
81 UsedNames.clear(); 89 UsedNames.clear();
82 Symbols.clear(); 90 Symbols.clear();
83 SectionSymbols.clear();
84 Allocator.Reset(); 91 Allocator.Reset();
85 Instances.clear(); 92 Instances.clear();
86 CompilationDir.clear(); 93 CompilationDir.clear();
87 MainFileName.clear(); 94 MainFileName.clear();
88 MCDwarfLineTablesCUMap.clear(); 95 MCDwarfLineTablesCUMap.clear();
122 Sym = createSymbol(NameRef, false, false); 129 Sym = createSymbol(NameRef, false, false);
123 130
124 return Sym; 131 return Sym;
125 } 132 }
126 133
127 MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
128 MCSymbolELF *&Sym = SectionSymbols[&Section];
129 if (Sym)
130 return Sym;
131
132 StringRef Name = Section.getSectionName();
133 auto NameIter = UsedNames.insert(std::make_pair(Name, false)).first;
134 Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
135
136 return Sym;
137 }
138
139 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName, 134 MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
140 unsigned Idx) { 135 unsigned Idx) {
141 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName + 136 return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
142 "$frame_escape_" + Twine(Idx)); 137 "$frame_escape_" + Twine(Idx));
143 } 138 }
160 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary); 155 return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
161 case MCObjectFileInfo::IsELF: 156 case MCObjectFileInfo::IsELF:
162 return new (Name, *this) MCSymbolELF(Name, IsTemporary); 157 return new (Name, *this) MCSymbolELF(Name, IsTemporary);
163 case MCObjectFileInfo::IsMachO: 158 case MCObjectFileInfo::IsMachO:
164 return new (Name, *this) MCSymbolMachO(Name, IsTemporary); 159 return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
160 case MCObjectFileInfo::IsWasm:
161 return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
165 } 162 }
166 } 163 }
167 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name, 164 return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
168 IsTemporary); 165 IsTemporary);
169 } 166 }
180 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix()); 177 IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
181 178
182 SmallString<128> NewName = Name; 179 SmallString<128> NewName = Name;
183 bool AddSuffix = AlwaysAddSuffix; 180 bool AddSuffix = AlwaysAddSuffix;
184 unsigned &NextUniqueID = NextID[Name]; 181 unsigned &NextUniqueID = NextID[Name];
185 for (;;) { 182 while (true) {
186 if (AddSuffix) { 183 if (AddSuffix) {
187 NewName.resize(Name.size()); 184 NewName.resize(Name.size());
188 raw_svector_ostream(NewName) << NextUniqueID++; 185 raw_svector_ostream(NewName) << NextUniqueID++;
189 } 186 }
190 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true)); 187 auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
258 SmallString<128> NameSV; 255 SmallString<128> NameSV;
259 StringRef NameRef = Name.toStringRef(NameSV); 256 StringRef NameRef = Name.toStringRef(NameSV);
260 return Symbols.lookup(NameRef); 257 return Symbols.lookup(NameRef);
261 } 258 }
262 259
260 void MCContext::setSymbolValue(MCStreamer &Streamer,
261 StringRef Sym,
262 uint64_t Val) {
263 auto Symbol = getOrCreateSymbol(Sym);
264 Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
265 }
266
263 //===----------------------------------------------------------------------===// 267 //===----------------------------------------------------------------------===//
264 // Section Management 268 // Section Management
265 //===----------------------------------------------------------------------===// 269 //===----------------------------------------------------------------------===//
266 270
267 MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section, 271 MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
268 unsigned TypeAndAttributes, 272 unsigned TypeAndAttributes,
269 unsigned Reserved2, SectionKind Kind, 273 unsigned Reserved2, SectionKind Kind,
270 const char *BeginSymName) { 274 const char *BeginSymName) {
271
272 // We unique sections by their segment/section pair. The returned section 275 // We unique sections by their segment/section pair. The returned section
273 // may not have the same flags as the requested section, if so this should be 276 // may not have the same flags as the requested section, if so this should be
274 // diagnosed by the client as an error. 277 // diagnosed by the client as an error.
275 278
276 // Form the name to look up. 279 // Form the name to look up.
307 .first; 310 .first;
308 StringRef CachedName = I->first.SectionName; 311 StringRef CachedName = I->first.SectionName;
309 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName); 312 const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
310 } 313 }
311 314
315 MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
316 unsigned Flags, SectionKind K,
317 unsigned EntrySize,
318 const MCSymbolELF *Group,
319 unsigned UniqueID,
320 const MCSymbolELF *Associated) {
321 MCSymbolELF *R;
322 MCSymbol *&Sym = Symbols[Section];
323 // A section symbol can not redefine regular symbols. There may be multiple
324 // sections with the same name, in which case the first such section wins.
325 if (Sym && Sym->isDefined() &&
326 (!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
327 reportError(SMLoc(), "invalid symbol redefinition");
328 if (Sym && Sym->isUndefined()) {
329 R = cast<MCSymbolELF>(Sym);
330 } else {
331 auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
332 R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
333 if (!Sym)
334 Sym = R;
335 }
336 R->setBinding(ELF::STB_LOCAL);
337 R->setType(ELF::STT_SECTION);
338
339 auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
340 Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
341
342 auto *F = new MCDataFragment();
343 Ret->getFragmentList().insert(Ret->begin(), F);
344 F->setParent(Ret);
345 R->setFragment(F);
346
347 return Ret;
348 }
349
312 MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type, 350 MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
313 unsigned Flags, unsigned EntrySize, 351 unsigned Flags, unsigned EntrySize,
314 const MCSymbolELF *Group, 352 const MCSymbolELF *Group,
315 const MCSectionELF *Associated) { 353 const MCSectionELF *RelInfoSection) {
316 StringMap<bool>::iterator I; 354 StringMap<bool>::iterator I;
317 bool Inserted; 355 bool Inserted;
318 std::tie(I, Inserted) = 356 std::tie(I, Inserted) =
319 ELFRelSecNames.insert(std::make_pair(Name.str(), true)); 357 RelSecNames.insert(std::make_pair(Name.str(), true));
320 358
321 return new (ELFAllocator.Allocate()) 359 return createELFSectionImpl(
322 MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(), 360 I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
323 EntrySize, Group, true, nullptr, Associated); 361 true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
324 } 362 }
325 363
326 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix, 364 MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
327 const Twine &Suffix, unsigned Type, 365 const Twine &Suffix, unsigned Type,
328 unsigned Flags, 366 unsigned Flags,
331 } 369 }
332 370
333 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, 371 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
334 unsigned Flags, unsigned EntrySize, 372 unsigned Flags, unsigned EntrySize,
335 const Twine &Group, unsigned UniqueID, 373 const Twine &Group, unsigned UniqueID,
336 const char *BeginSymName) { 374 const MCSymbolELF *Associated) {
337 MCSymbolELF *GroupSym = nullptr; 375 MCSymbolELF *GroupSym = nullptr;
338 if (!Group.isTriviallyEmpty() && !Group.str().empty()) 376 if (!Group.isTriviallyEmpty() && !Group.str().empty())
339 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group)); 377 GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
340 378
341 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID, 379 return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
342 BeginSymName, nullptr); 380 Associated);
343 } 381 }
344 382
345 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type, 383 MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
346 unsigned Flags, unsigned EntrySize, 384 unsigned Flags, unsigned EntrySize,
347 const MCSymbolELF *GroupSym, 385 const MCSymbolELF *GroupSym,
348 unsigned UniqueID, 386 unsigned UniqueID,
349 const char *BeginSymName, 387 const MCSymbolELF *Associated) {
350 const MCSectionELF *Associated) {
351 StringRef Group = ""; 388 StringRef Group = "";
352 if (GroupSym) 389 if (GroupSym)
353 Group = GroupSym->getName(); 390 Group = GroupSym->getName();
354 // Do the lookup, if we have a hit, return it. 391 // Do the lookup, if we have a hit, return it.
355 auto IterBool = ELFUniquingMap.insert( 392 auto IterBool = ELFUniquingMap.insert(
359 return Entry.second; 396 return Entry.second;
360 397
361 StringRef CachedName = Entry.first.SectionName; 398 StringRef CachedName = Entry.first.SectionName;
362 399
363 SectionKind Kind; 400 SectionKind Kind;
364 if (Flags & ELF::SHF_EXECINSTR) 401 if (Flags & ELF::SHF_ARM_PURECODE)
402 Kind = SectionKind::getExecuteOnly();
403 else if (Flags & ELF::SHF_EXECINSTR)
365 Kind = SectionKind::getText(); 404 Kind = SectionKind::getText();
366 else 405 else
367 Kind = SectionKind::getReadOnly(); 406 Kind = SectionKind::getReadOnly();
368 407
369 MCSymbol *Begin = nullptr; 408 MCSectionELF *Result = createELFSectionImpl(
370 if (BeginSymName) 409 CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
371 Begin = createTempSymbol(BeginSymName, false);
372
373 MCSectionELF *Result = new (ELFAllocator.Allocate())
374 MCSectionELF(CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID,
375 Begin, Associated);
376 Entry.second = Result; 410 Entry.second = Result;
377 return Result; 411 return Result;
378 } 412 }
379 413
380 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) { 414 MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
381 MCSectionELF *Result = new (ELFAllocator.Allocate()) 415 return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
382 MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4, 416 SectionKind::getReadOnly(), 4, Group, ~0,
383 Group, ~0, nullptr, nullptr); 417 nullptr);
384 return Result;
385 } 418 }
386 419
387 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section, 420 MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
388 unsigned Characteristics, 421 unsigned Characteristics,
389 SectionKind Kind, 422 SectionKind Kind,
451 484
452 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(), 485 return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
453 "", 0, UniqueID); 486 "", 0, UniqueID);
454 } 487 }
455 488
489 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
490 const Twine &Group, unsigned UniqueID,
491 const char *BeginSymName) {
492 MCSymbolWasm *GroupSym = nullptr;
493 if (!Group.isTriviallyEmpty() && !Group.str().empty())
494 GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
495
496 return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
497 }
498
499 MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
500 const MCSymbolWasm *GroupSym,
501 unsigned UniqueID,
502 const char *BeginSymName) {
503 StringRef Group = "";
504 if (GroupSym)
505 Group = GroupSym->getName();
506 // Do the lookup, if we have a hit, return it.
507 auto IterBool = WasmUniquingMap.insert(
508 std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
509 auto &Entry = *IterBool.first;
510 if (!IterBool.second)
511 return Entry.second;
512
513 StringRef CachedName = Entry.first.SectionName;
514
515 MCSymbol *Begin = nullptr;
516 if (BeginSymName)
517 Begin = createTempSymbol(BeginSymName, false);
518
519 MCSectionWasm *Result = new (WasmAllocator.Allocate())
520 MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
521 Entry.second = Result;
522 return Result;
523 }
524
456 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) { 525 MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
457 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI); 526 return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
458 } 527 }
459 528
460 //===----------------------------------------------------------------------===// 529 //===----------------------------------------------------------------------===//
499 //===----------------------------------------------------------------------===// 568 //===----------------------------------------------------------------------===//
500 569
501 void MCContext::reportError(SMLoc Loc, const Twine &Msg) { 570 void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
502 HadError = true; 571 HadError = true;
503 572
504 // If we have a source manager use it. Otherwise just use the generic 573 // If we have a source manager use it. Otherwise, try using the inline source
505 // report_fatal_error(). 574 // manager.
506 if (!SrcMgr) 575 // If that fails, use the generic report_fatal_error().
576 if (SrcMgr)
577 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
578 else if (InlineSrcMgr)
579 InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
580 else
507 report_fatal_error(Msg, false); 581 report_fatal_error(Msg, false);
508
509 // Use the source manager to print the message.
510 SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
511 } 582 }
512 583
513 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) { 584 void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
514 reportError(Loc, Msg); 585 reportError(Loc, Msg);
515 586