2009-06-24 07:39:15 +08:00
|
|
|
//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
|
2009-06-24 06:01:43 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2017-02-14 08:33:36 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/StringMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Twine.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/COFF.h"
|
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2010-03-12 06:56:10 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2016-01-29 08:49:42 +08:00
|
|
|
#include "llvm/MC/MCCodeView.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
2017-02-14 08:33:36 +08:00
|
|
|
#include "llvm/MC/MCExpr.h"
|
|
|
|
#include "llvm/MC/MCFragment.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCLabel.h"
|
2011-07-20 13:58:47 +08:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2010-05-08 01:17:41 +08:00
|
|
|
#include "llvm/MC/MCSectionCOFF.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/MC/MCSectionWasm.h"
|
2015-03-04 09:24:11 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2017-02-14 08:33:36 +08:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2015-06-09 01:17:12 +08:00
|
|
|
#include "llvm/MC/MCSymbolCOFF.h"
|
2015-06-02 08:25:12 +08:00
|
|
|
#include "llvm/MC/MCSymbolELF.h"
|
2015-06-09 01:17:23 +08:00
|
|
|
#include "llvm/MC/MCSymbolMachO.h"
|
2017-02-22 09:23:18 +08:00
|
|
|
#include "llvm/MC/MCSymbolWasm.h"
|
2017-02-14 08:33:36 +08:00
|
|
|
#include "llvm/MC/SectionKind.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/Casting.h"
|
2016-06-17 23:19:41 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2012-01-27 07:20:11 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2012-12-18 08:31:01 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2012-01-27 07:20:11 +08:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2017-02-14 08:33:36 +08:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <tuple>
|
|
|
|
#include <utility>
|
2013-10-23 07:41:52 +08:00
|
|
|
|
2009-06-24 06:01:43 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2016-06-17 23:19:41 +08:00
|
|
|
static cl::opt<char*>
|
|
|
|
AsSecureLogFileName("as-secure-log-file-name",
|
|
|
|
cl::desc("As secure log file name (initialized from "
|
|
|
|
"AS_SECURE_LOG_FILE env variable)"),
|
|
|
|
cl::init(getenv("AS_SECURE_LOG_FILE")), cl::Hidden);
|
|
|
|
|
2013-06-18 15:20:20 +08:00
|
|
|
MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
|
2012-12-07 06:12:44 +08:00
|
|
|
const MCObjectFileInfo *mofi, const SourceMgr *mgr,
|
2014-03-28 04:45:58 +08:00
|
|
|
bool DoAutoReset)
|
2017-02-25 05:44:52 +08:00
|
|
|
: SrcMgr(mgr), InlineSrcMgr(nullptr), MAI(mai), MRI(mri), MOFI(mofi),
|
|
|
|
Symbols(Allocator), UsedNames(Allocator),
|
|
|
|
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0),
|
2017-02-14 08:33:36 +08:00
|
|
|
AutoReset(DoAutoReset) {
|
2016-06-17 23:19:41 +08:00
|
|
|
SecureLogFile = AsSecureLogFileName;
|
2012-12-18 08:31:01 +08:00
|
|
|
|
2014-07-06 18:33:31 +08:00
|
|
|
if (SrcMgr && SrcMgr->getNumBuffers())
|
|
|
|
MainFileName =
|
|
|
|
SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
|
2009-06-24 06:01:43 +08:00
|
|
|
}
|
|
|
|
|
2012-12-06 10:00:13 +08:00
|
|
|
MCContext::~MCContext() {
|
2012-12-13 06:59:46 +08:00
|
|
|
if (AutoReset)
|
|
|
|
reset();
|
2012-12-07 06:12:44 +08:00
|
|
|
|
2012-12-06 10:00:13 +08:00
|
|
|
// NOTE: The symbols are all allocated out of a bump pointer allocator,
|
|
|
|
// we don't need to free them here.
|
2012-12-07 06:12:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Module Lifetime Management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-13 06:59:46 +08:00
|
|
|
void MCContext::reset() {
|
2015-05-26 09:52:19 +08:00
|
|
|
// Call the destructors so the fragments are freed
|
2015-10-08 03:08:19 +08:00
|
|
|
COFFAllocator.DestroyAll();
|
|
|
|
ELFAllocator.DestroyAll();
|
|
|
|
MachOAllocator.DestroyAll();
|
2015-05-26 09:52:19 +08:00
|
|
|
|
2015-11-14 14:35:56 +08:00
|
|
|
MCSubtargetAllocator.DestroyAll();
|
2012-12-07 06:12:44 +08:00
|
|
|
UsedNames.clear();
|
|
|
|
Symbols.clear();
|
|
|
|
Allocator.Reset();
|
|
|
|
Instances.clear();
|
2014-09-17 17:25:36 +08:00
|
|
|
CompilationDir.clear();
|
|
|
|
MainFileName.clear();
|
2014-03-14 05:59:51 +08:00
|
|
|
MCDwarfLineTablesCUMap.clear();
|
2015-05-22 00:52:32 +08:00
|
|
|
SectionsForRanges.clear();
|
2012-12-07 06:12:44 +08:00
|
|
|
MCGenDwarfLabelEntries.clear();
|
|
|
|
DwarfDebugFlags = StringRef();
|
2013-02-20 08:10:29 +08:00
|
|
|
DwarfCompileUnitID = 0;
|
2015-05-19 02:43:23 +08:00
|
|
|
CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
CVContext.reset();
|
|
|
|
|
2014-04-11 07:55:11 +08:00
|
|
|
MachOUniquingMap.clear();
|
|
|
|
ELFUniquingMap.clear();
|
|
|
|
COFFUniquingMap.clear();
|
2012-12-13 06:59:46 +08:00
|
|
|
|
2015-03-18 04:07:06 +08:00
|
|
|
NextID.clear();
|
2012-12-13 06:59:46 +08:00
|
|
|
AllowTemporaryLabels = true;
|
|
|
|
DwarfLocSeen = false;
|
|
|
|
GenDwarfForAssembly = false;
|
|
|
|
GenDwarfFileNumber = 0;
|
2015-11-17 17:58:07 +08:00
|
|
|
|
|
|
|
HadError = false;
|
2009-06-24 06:01:43 +08:00
|
|
|
}
|
|
|
|
|
2010-04-09 04:30:37 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbol Manipulation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
|
2015-03-17 17:51:17 +08:00
|
|
|
SmallString<128> NameSV;
|
|
|
|
StringRef NameRef = Name.toStringRef(NameSV);
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2015-03-17 17:51:17 +08:00
|
|
|
assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
|
2010-12-02 04:46:11 +08:00
|
|
|
|
2015-03-17 17:51:17 +08:00
|
|
|
MCSymbol *&Sym = Symbols[NameRef];
|
2014-11-19 13:49:42 +08:00
|
|
|
if (!Sym)
|
2015-06-23 19:31:32 +08:00
|
|
|
Sym = createSymbol(NameRef, false, false);
|
2010-12-02 04:46:11 +08:00
|
|
|
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-03-06 02:26:34 +08:00
|
|
|
MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
|
|
|
|
unsigned Idx) {
|
2015-05-19 02:43:14 +08:00
|
|
|
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
|
2015-03-06 02:26:34 +08:00
|
|
|
"$frame_escape_" + Twine(Idx));
|
2015-01-13 08:48:10 +08:00
|
|
|
}
|
|
|
|
|
2015-04-01 06:35:44 +08:00
|
|
|
MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
|
2015-05-19 02:43:14 +08:00
|
|
|
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
|
2015-04-01 06:35:44 +08:00
|
|
|
"$parent_frame_offset");
|
|
|
|
}
|
|
|
|
|
2015-05-21 07:08:04 +08:00
|
|
|
MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
|
|
|
|
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
|
|
|
|
FuncName);
|
|
|
|
}
|
|
|
|
|
2015-06-02 08:25:12 +08:00
|
|
|
MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
|
|
|
|
bool IsTemporary) {
|
2015-06-09 01:17:12 +08:00
|
|
|
if (MOFI) {
|
2015-08-14 23:48:41 +08:00
|
|
|
switch (MOFI->getObjectFileType()) {
|
|
|
|
case MCObjectFileInfo::IsCOFF:
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
|
2015-08-14 23:48:41 +08:00
|
|
|
case MCObjectFileInfo::IsELF:
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbolELF(Name, IsTemporary);
|
2015-08-14 23:48:41 +08:00
|
|
|
case MCObjectFileInfo::IsMachO:
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
|
2017-02-22 09:23:18 +08:00
|
|
|
case MCObjectFileInfo::IsWasm:
|
|
|
|
return new (Name, *this) MCSymbolWasm(Name, IsTemporary);
|
2015-06-09 01:17:12 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
|
|
|
|
IsTemporary);
|
2015-06-02 08:25:12 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 19:31:32 +08:00
|
|
|
MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
|
|
|
|
bool CanBeUnnamed) {
|
|
|
|
if (CanBeUnnamed && !UseNamesOnTempLabels)
|
|
|
|
return createSymbolImpl(nullptr, true);
|
|
|
|
|
2016-09-29 10:03:44 +08:00
|
|
|
// Determine whether this is a user written assembler temporary or normal
|
2015-06-03 06:52:13 +08:00
|
|
|
// label, if used.
|
2015-06-23 19:31:32 +08:00
|
|
|
bool IsTemporary = CanBeUnnamed;
|
|
|
|
if (AllowTemporaryLabels && !IsTemporary)
|
2015-03-18 04:07:06 +08:00
|
|
|
IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
|
2010-12-02 04:46:11 +08:00
|
|
|
|
2015-03-18 04:07:06 +08:00
|
|
|
SmallString<128> NewName = Name;
|
|
|
|
bool AddSuffix = AlwaysAddSuffix;
|
|
|
|
unsigned &NextUniqueID = NextID[Name];
|
2017-02-14 08:33:36 +08:00
|
|
|
while (true) {
|
2015-03-18 04:07:06 +08:00
|
|
|
if (AddSuffix) {
|
2011-04-09 19:26:27 +08:00
|
|
|
NewName.resize(Name.size());
|
|
|
|
raw_svector_ostream(NewName) << NextUniqueID++;
|
2015-03-18 04:07:06 +08:00
|
|
|
}
|
|
|
|
auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
|
2016-03-29 04:36:28 +08:00
|
|
|
if (NameEntry.second || !NameEntry.first->second) {
|
|
|
|
// Ok, we found a name.
|
|
|
|
// Mark it as used for a non-section symbol.
|
|
|
|
NameEntry.first->second = true;
|
|
|
|
// Have the MCSymbol object itself refer to the copy of the string that is
|
|
|
|
// embedded in the UsedNames entry.
|
2015-06-02 08:25:12 +08:00
|
|
|
return createSymbolImpl(&*NameEntry.first, IsTemporary);
|
2015-03-18 04:07:06 +08:00
|
|
|
}
|
|
|
|
assert(IsTemporary && "Cannot rename non-temporary symbols");
|
|
|
|
AddSuffix = true;
|
2010-12-02 04:46:11 +08:00
|
|
|
}
|
2015-03-18 04:07:06 +08:00
|
|
|
llvm_unreachable("Infinite loop");
|
2009-06-24 12:31:49 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 19:31:32 +08:00
|
|
|
MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
|
|
|
|
bool CanBeUnnamed) {
|
2015-02-28 02:18:39 +08:00
|
|
|
SmallString<128> NameSV;
|
|
|
|
raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
|
2015-06-23 19:31:32 +08:00
|
|
|
return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
|
2015-02-28 02:18:39 +08:00
|
|
|
}
|
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
|
2014-06-27 06:52:05 +08:00
|
|
|
SmallString<128> NameSV;
|
2015-03-18 04:07:06 +08:00
|
|
|
raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
|
2015-06-23 19:31:32 +08:00
|
|
|
return createSymbol(NameSV, true, false);
|
2014-03-29 15:05:06 +08:00
|
|
|
}
|
|
|
|
|
2015-06-23 19:31:32 +08:00
|
|
|
MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
|
|
|
|
return createTempSymbol("tmp", true, CanBeUnnamed);
|
2010-03-10 09:29:27 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 02:09:26 +08:00
|
|
|
unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
|
2010-05-18 20:15:34 +08:00
|
|
|
MCLabel *&Label = Instances[LocalLabelVal];
|
|
|
|
if (!Label)
|
|
|
|
Label = new (*this) MCLabel(0);
|
|
|
|
return Label->incInstance();
|
2010-05-18 07:08:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 02:09:26 +08:00
|
|
|
unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
|
2010-05-18 20:15:34 +08:00
|
|
|
MCLabel *&Label = Instances[LocalLabelVal];
|
|
|
|
if (!Label)
|
|
|
|
Label = new (*this) MCLabel(0);
|
|
|
|
return Label->getInstance();
|
2010-05-18 07:08:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-14 02:09:26 +08:00
|
|
|
MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
|
|
|
|
unsigned Instance) {
|
|
|
|
MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
|
|
|
|
if (!Sym)
|
2015-06-23 19:31:32 +08:00
|
|
|
Sym = createTempSymbol(false);
|
2014-03-14 02:09:26 +08:00
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
|
2014-03-14 02:09:26 +08:00
|
|
|
unsigned Instance = NextInstance(LocalLabelVal);
|
|
|
|
return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
|
2010-05-18 07:08:19 +08:00
|
|
|
}
|
2014-03-14 02:09:26 +08:00
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
|
2014-03-14 02:09:26 +08:00
|
|
|
bool Before) {
|
|
|
|
unsigned Instance = GetInstance(LocalLabelVal);
|
|
|
|
if (!Before)
|
|
|
|
++Instance;
|
|
|
|
return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
|
2010-05-18 07:08:19 +08:00
|
|
|
}
|
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
|
2012-09-19 01:10:37 +08:00
|
|
|
SmallString<128> NameSV;
|
2015-03-18 02:55:30 +08:00
|
|
|
StringRef NameRef = Name.toStringRef(NameSV);
|
|
|
|
return Symbols.lookup(NameRef);
|
2012-09-19 01:10:37 +08:00
|
|
|
}
|
|
|
|
|
2016-12-06 10:49:17 +08:00
|
|
|
void MCContext::setSymbolValue(MCStreamer &Streamer,
|
|
|
|
StringRef Sym,
|
|
|
|
uint64_t Val) {
|
|
|
|
auto Symbol = getOrCreateSymbol(Sym);
|
|
|
|
Streamer.EmitAssignment(Symbol, MCConstantExpr::create(Val, *this));
|
2016-12-02 02:42:04 +08:00
|
|
|
}
|
|
|
|
|
2010-04-09 04:30:37 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Section Management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
|
|
|
|
unsigned TypeAndAttributes,
|
|
|
|
unsigned Reserved2, SectionKind Kind,
|
|
|
|
const char *BeginSymName) {
|
2010-04-09 04:30:37 +08:00
|
|
|
// We unique sections by their segment/section pair. The returned section
|
|
|
|
// may not have the same flags as the requested section, if so this should be
|
|
|
|
// diagnosed by the client as an error.
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2010-04-09 04:30:37 +08:00
|
|
|
// Form the name to look up.
|
|
|
|
SmallString<64> Name;
|
|
|
|
Name += Segment;
|
|
|
|
Name.push_back(',');
|
|
|
|
Name += Section;
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2010-04-09 04:30:37 +08:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionMachO *&Entry = MachOUniquingMap[Name];
|
2015-03-11 05:16:18 +08:00
|
|
|
if (Entry)
|
|
|
|
return Entry;
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2015-03-11 06:00:25 +08:00
|
|
|
MCSymbol *Begin = nullptr;
|
|
|
|
if (BeginSymName)
|
2015-03-18 04:07:06 +08:00
|
|
|
Begin = createTempSymbol(BeginSymName, false);
|
2015-03-11 06:00:25 +08:00
|
|
|
|
2010-04-09 04:30:37 +08:00
|
|
|
// Otherwise, return a new section.
|
2015-10-08 03:08:19 +08:00
|
|
|
return Entry = new (MachOAllocator.Allocate()) MCSectionMachO(
|
|
|
|
Segment, Section, TypeAndAttributes, Reserved2, Kind, Begin);
|
2010-04-09 04:30:37 +08:00
|
|
|
}
|
2010-04-09 05:26:26 +08:00
|
|
|
|
2016-05-25 08:14:12 +08:00
|
|
|
void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
|
|
|
|
StringRef GroupName;
|
|
|
|
if (const MCSymbol *Group = Section->getGroup())
|
|
|
|
GroupName = Group->getName();
|
|
|
|
|
|
|
|
unsigned UniqueID = Section->getUniqueID();
|
|
|
|
ELFUniquingMap.erase(
|
|
|
|
ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
|
|
|
|
auto I = ELFUniquingMap.insert(std::make_pair(
|
|
|
|
ELFSectionKey{Name, GroupName, UniqueID},
|
|
|
|
Section))
|
|
|
|
.first;
|
|
|
|
StringRef CachedName = I->first.SectionName;
|
|
|
|
const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
|
|
|
|
}
|
|
|
|
|
2017-02-03 05:26:06 +08:00
|
|
|
MCSectionELF *MCContext::createELFSectionImpl(StringRef Section, unsigned Type,
|
|
|
|
unsigned Flags, SectionKind K,
|
|
|
|
unsigned EntrySize,
|
|
|
|
const MCSymbolELF *Group,
|
|
|
|
unsigned UniqueID,
|
2017-03-15 03:28:51 +08:00
|
|
|
const MCSymbolELF *Associated) {
|
2017-02-03 05:26:06 +08:00
|
|
|
MCSymbolELF *R;
|
|
|
|
MCSymbol *&Sym = Symbols[Section];
|
2017-02-25 05:44:58 +08:00
|
|
|
// A section symbol can not redefine regular symbols. There may be multiple
|
|
|
|
// sections with the same name, in which case the first such section wins.
|
|
|
|
if (Sym && Sym->isDefined() &&
|
|
|
|
(!Sym->isInSection() || Sym->getSection().getBeginSymbol() != Sym))
|
|
|
|
reportError(SMLoc(), "invalid symbol redefinition");
|
2017-02-03 05:26:06 +08:00
|
|
|
if (Sym && Sym->isUndefined()) {
|
|
|
|
R = cast<MCSymbolELF>(Sym);
|
|
|
|
} else {
|
|
|
|
auto NameIter = UsedNames.insert(std::make_pair(Section, false)).first;
|
|
|
|
R = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
|
|
|
|
if (!Sym)
|
|
|
|
Sym = R;
|
|
|
|
}
|
|
|
|
R->setBinding(ELF::STB_LOCAL);
|
|
|
|
R->setType(ELF::STT_SECTION);
|
|
|
|
|
|
|
|
auto *Ret = new (ELFAllocator.Allocate()) MCSectionELF(
|
|
|
|
Section, Type, Flags, K, EntrySize, Group, UniqueID, R, Associated);
|
|
|
|
|
|
|
|
auto *F = new MCDataFragment();
|
|
|
|
Ret->getFragmentList().insert(Ret->begin(), F);
|
|
|
|
F->setParent(Ret);
|
|
|
|
R->setFragment(F);
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2016-07-01 14:07:38 +08:00
|
|
|
MCSectionELF *MCContext::createELFRelSection(const Twine &Name, unsigned Type,
|
2015-05-22 03:20:38 +08:00
|
|
|
unsigned Flags, unsigned EntrySize,
|
2015-06-03 05:30:13 +08:00
|
|
|
const MCSymbolELF *Group,
|
2017-03-15 03:28:51 +08:00
|
|
|
const MCSectionELF *RelInfoSection) {
|
2015-03-30 21:39:16 +08:00
|
|
|
StringMap<bool>::iterator I;
|
|
|
|
bool Inserted;
|
2016-07-01 14:07:38 +08:00
|
|
|
std::tie(I, Inserted) =
|
2017-02-22 09:23:18 +08:00
|
|
|
RelSecNames.insert(std::make_pair(Name.str(), true));
|
2015-03-30 21:39:16 +08:00
|
|
|
|
2017-03-15 03:28:51 +08:00
|
|
|
return createELFSectionImpl(
|
|
|
|
I->getKey(), Type, Flags, SectionKind::getReadOnly(), EntrySize, Group,
|
|
|
|
true, cast<MCSymbolELF>(RelInfoSection->getBeginSymbol()));
|
2015-03-30 21:39:16 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 14:07:38 +08:00
|
|
|
MCSectionELF *MCContext::getELFNamedSection(const Twine &Prefix,
|
|
|
|
const Twine &Suffix, unsigned Type,
|
|
|
|
unsigned Flags,
|
|
|
|
unsigned EntrySize) {
|
|
|
|
return getELFSection(Prefix + "." + Suffix, Type, Flags, EntrySize, Suffix);
|
|
|
|
}
|
|
|
|
|
|
|
|
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
|
2015-05-22 03:20:38 +08:00
|
|
|
unsigned Flags, unsigned EntrySize,
|
2017-02-09 22:59:20 +08:00
|
|
|
const Twine &Group, unsigned UniqueID,
|
2017-03-15 03:28:51 +08:00
|
|
|
const MCSymbolELF *Associated) {
|
2015-06-03 05:30:13 +08:00
|
|
|
MCSymbolELF *GroupSym = nullptr;
|
2016-07-01 14:07:38 +08:00
|
|
|
if (!Group.isTriviallyEmpty() && !Group.str().empty())
|
2015-06-03 05:30:13 +08:00
|
|
|
GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
|
2015-03-30 21:59:06 +08:00
|
|
|
|
2015-04-06 12:25:18 +08:00
|
|
|
return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
|
2017-02-09 22:59:20 +08:00
|
|
|
Associated);
|
2015-04-06 12:25:18 +08:00
|
|
|
}
|
|
|
|
|
2016-07-01 14:07:38 +08:00
|
|
|
MCSectionELF *MCContext::getELFSection(const Twine &Section, unsigned Type,
|
2015-05-22 03:20:38 +08:00
|
|
|
unsigned Flags, unsigned EntrySize,
|
2015-06-03 05:30:13 +08:00
|
|
|
const MCSymbolELF *GroupSym,
|
2015-05-22 03:20:38 +08:00
|
|
|
unsigned UniqueID,
|
2017-03-15 03:28:51 +08:00
|
|
|
const MCSymbolELF *Associated) {
|
2015-04-06 12:25:18 +08:00
|
|
|
StringRef Group = "";
|
|
|
|
if (GroupSym)
|
|
|
|
Group = GroupSym->getName();
|
2010-04-09 05:26:26 +08:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2014-04-11 07:55:11 +08:00
|
|
|
auto IterBool = ELFUniquingMap.insert(
|
2016-07-01 14:07:38 +08:00
|
|
|
std::make_pair(ELFSectionKey{Section.str(), Group, UniqueID}, nullptr));
|
2014-04-11 07:55:11 +08:00
|
|
|
auto &Entry = *IterBool.first;
|
2015-04-05 02:02:01 +08:00
|
|
|
if (!IterBool.second)
|
2015-02-18 04:48:01 +08:00
|
|
|
return Entry.second;
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2015-03-28 05:34:24 +08:00
|
|
|
StringRef CachedName = Entry.first.SectionName;
|
2015-01-30 01:33:21 +08:00
|
|
|
|
|
|
|
SectionKind Kind;
|
2016-12-15 15:59:08 +08:00
|
|
|
if (Flags & ELF::SHF_ARM_PURECODE)
|
|
|
|
Kind = SectionKind::getExecuteOnly();
|
|
|
|
else if (Flags & ELF::SHF_EXECINSTR)
|
2015-01-30 01:33:21 +08:00
|
|
|
Kind = SectionKind::getText();
|
|
|
|
else
|
|
|
|
Kind = SectionKind::getReadOnly();
|
|
|
|
|
2017-02-03 05:26:06 +08:00
|
|
|
MCSectionELF *Result = createELFSectionImpl(
|
|
|
|
CachedName, Type, Flags, Kind, EntrySize, GroupSym, UniqueID, Associated);
|
2015-04-05 02:02:01 +08:00
|
|
|
Entry.second = Result;
|
2010-04-09 05:26:26 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-06-03 05:30:13 +08:00
|
|
|
MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
|
2017-02-03 05:26:06 +08:00
|
|
|
return createELFSectionImpl(".group", ELF::SHT_GROUP, 0,
|
|
|
|
SectionKind::getReadOnly(), 4, Group, ~0,
|
|
|
|
nullptr);
|
2010-11-12 02:13:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
|
|
|
|
unsigned Characteristics,
|
|
|
|
SectionKind Kind,
|
|
|
|
StringRef COMDATSymName, int Selection,
|
2016-05-03 07:22:18 +08:00
|
|
|
unsigned UniqueID,
|
2015-05-22 03:20:38 +08:00
|
|
|
const char *BeginSymName) {
|
2015-03-30 21:59:06 +08:00
|
|
|
MCSymbol *COMDATSymbol = nullptr;
|
|
|
|
if (!COMDATSymName.empty()) {
|
2015-05-19 02:43:14 +08:00
|
|
|
COMDATSymbol = getOrCreateSymbol(COMDATSymName);
|
2015-03-30 21:59:06 +08:00
|
|
|
COMDATSymName = COMDATSymbol->getName();
|
|
|
|
}
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2016-05-03 07:22:18 +08:00
|
|
|
|
2015-03-30 21:59:06 +08:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2016-05-03 07:22:18 +08:00
|
|
|
COFFSectionKey T{Section, COMDATSymName, Selection, UniqueID};
|
2014-06-28 01:19:44 +08:00
|
|
|
auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
|
2014-04-11 07:55:11 +08:00
|
|
|
auto Iter = IterBool.first;
|
|
|
|
if (!IterBool.second)
|
2013-11-20 03:52:52 +08:00
|
|
|
return Iter->second;
|
|
|
|
|
2015-03-11 06:00:25 +08:00
|
|
|
MCSymbol *Begin = nullptr;
|
|
|
|
if (BeginSymName)
|
2015-03-18 04:07:06 +08:00
|
|
|
Begin = createTempSymbol(BeginSymName, false);
|
2015-03-11 06:00:25 +08:00
|
|
|
|
2015-03-28 05:34:24 +08:00
|
|
|
StringRef CachedName = Iter->first.SectionName;
|
2015-10-08 03:08:19 +08:00
|
|
|
MCSectionCOFF *Result = new (COFFAllocator.Allocate()) MCSectionCOFF(
|
2015-03-11 06:00:25 +08:00
|
|
|
CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
|
2010-11-26 12:16:08 +08:00
|
|
|
|
2013-11-20 03:52:52 +08:00
|
|
|
Iter->second = Result;
|
2010-05-08 01:17:41 +08:00
|
|
|
return Result;
|
|
|
|
}
|
2010-07-29 04:55:35 +08:00
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
|
|
|
|
unsigned Characteristics,
|
|
|
|
SectionKind Kind,
|
|
|
|
const char *BeginSymName) {
|
2016-05-03 07:22:18 +08:00
|
|
|
return getCOFFSection(Section, Characteristics, Kind, "", 0, GenericSectionID,
|
|
|
|
BeginSymName);
|
2013-11-20 03:52:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
|
2016-05-03 07:22:18 +08:00
|
|
|
COFFSectionKey T{Section, "", 0, GenericSectionID};
|
2014-06-28 01:19:44 +08:00
|
|
|
auto Iter = COFFUniquingMap.find(T);
|
2014-04-11 07:55:11 +08:00
|
|
|
if (Iter == COFFUniquingMap.end())
|
2014-04-13 12:57:38 +08:00
|
|
|
return nullptr;
|
2013-11-20 03:52:52 +08:00
|
|
|
return Iter->second;
|
2013-07-06 20:13:10 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
|
2016-05-03 07:22:18 +08:00
|
|
|
const MCSymbol *KeySym,
|
|
|
|
unsigned UniqueID) {
|
|
|
|
// Return the normal section if we don't have to be associative or unique.
|
|
|
|
if (!KeySym && UniqueID == GenericSectionID)
|
2014-09-05 01:42:03 +08:00
|
|
|
return Sec;
|
|
|
|
|
2016-05-03 07:22:18 +08:00
|
|
|
// If we have a key symbol, make an associative section with the same name and
|
|
|
|
// kind as the normal section.
|
|
|
|
unsigned Characteristics = Sec->getCharacteristics();
|
|
|
|
if (KeySym) {
|
|
|
|
Characteristics |= COFF::IMAGE_SCN_LNK_COMDAT;
|
|
|
|
return getCOFFSection(Sec->getSectionName(), Characteristics,
|
|
|
|
Sec->getKind(), KeySym->getName(),
|
|
|
|
COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID);
|
|
|
|
}
|
|
|
|
|
2014-09-05 01:42:03 +08:00
|
|
|
return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
|
2016-05-03 07:22:18 +08:00
|
|
|
"", 0, UniqueID);
|
2015-11-14 14:35:56 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 05:28:38 +08:00
|
|
|
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind K,
|
2017-02-22 09:23:18 +08:00
|
|
|
const Twine &Group, unsigned UniqueID,
|
|
|
|
const char *BeginSymName) {
|
|
|
|
MCSymbolWasm *GroupSym = nullptr;
|
2018-01-12 04:35:17 +08:00
|
|
|
if (!Group.isTriviallyEmpty() && !Group.str().empty()) {
|
2017-02-22 09:23:18 +08:00
|
|
|
GroupSym = cast<MCSymbolWasm>(getOrCreateSymbol(Group));
|
2018-01-12 04:35:17 +08:00
|
|
|
GroupSym->setComdat(true);
|
|
|
|
}
|
2017-02-22 09:23:18 +08:00
|
|
|
|
2017-10-21 05:28:38 +08:00
|
|
|
return getWasmSection(Section, K, GroupSym, UniqueID, BeginSymName);
|
2017-02-22 09:23:18 +08:00
|
|
|
}
|
|
|
|
|
2017-10-21 05:28:38 +08:00
|
|
|
MCSectionWasm *MCContext::getWasmSection(const Twine &Section, SectionKind Kind,
|
2017-02-22 09:23:18 +08:00
|
|
|
const MCSymbolWasm *GroupSym,
|
|
|
|
unsigned UniqueID,
|
|
|
|
const char *BeginSymName) {
|
|
|
|
StringRef Group = "";
|
|
|
|
if (GroupSym)
|
|
|
|
Group = GroupSym->getName();
|
|
|
|
// Do the lookup, if we have a hit, return it.
|
|
|
|
auto IterBool = WasmUniquingMap.insert(
|
|
|
|
std::make_pair(WasmSectionKey{Section.str(), Group, UniqueID}, nullptr));
|
|
|
|
auto &Entry = *IterBool.first;
|
|
|
|
if (!IterBool.second)
|
|
|
|
return Entry.second;
|
|
|
|
|
|
|
|
StringRef CachedName = Entry.first.SectionName;
|
|
|
|
|
|
|
|
MCSymbol *Begin = nullptr;
|
|
|
|
if (BeginSymName)
|
|
|
|
Begin = createTempSymbol(BeginSymName, false);
|
|
|
|
|
|
|
|
MCSectionWasm *Result = new (WasmAllocator.Allocate())
|
2017-10-21 05:28:38 +08:00
|
|
|
MCSectionWasm(CachedName, Kind, GroupSym, UniqueID, Begin);
|
2017-02-22 09:23:18 +08:00
|
|
|
Entry.second = Result;
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-11-14 14:35:56 +08:00
|
|
|
MCSubtargetInfo &MCContext::getSubtargetCopy(const MCSubtargetInfo &STI) {
|
|
|
|
return *new (MCSubtargetAllocator.Allocate()) MCSubtargetInfo(STI);
|
2014-09-05 01:42:03 +08:00
|
|
|
}
|
|
|
|
|
2010-07-29 04:55:35 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dwarf Management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
/// getDwarfFile - takes a file name an number to place in the dwarf file and
|
2010-07-29 04:55:35 +08:00
|
|
|
/// directory tables. If the file number has already been allocated it is an
|
|
|
|
/// error and zero is returned and the client reports the error, else the
|
|
|
|
/// allocated file number is returned. The file numbers may be in any order.
|
2015-05-19 02:43:14 +08:00
|
|
|
unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
|
2018-01-10 07:31:48 +08:00
|
|
|
unsigned FileNumber, MD5::MD5Result *Checksum,
|
|
|
|
unsigned CUID) {
|
2014-03-14 05:59:51 +08:00
|
|
|
MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
|
2018-01-10 07:31:48 +08:00
|
|
|
return Table.getFile(Directory, FileName, Checksum, FileNumber);
|
2010-07-29 04:55:35 +08:00
|
|
|
}
|
2010-08-25 04:32:42 +08:00
|
|
|
|
2010-10-05 04:17:24 +08:00
|
|
|
/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
|
2010-08-25 04:32:42 +08:00
|
|
|
/// currently is assigned and false otherwise.
|
2013-03-07 09:42:00 +08:00
|
|
|
bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
|
2015-05-19 02:43:23 +08:00
|
|
|
const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
|
|
|
|
if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
|
2010-08-25 04:32:42 +08:00
|
|
|
return false;
|
|
|
|
|
2014-03-14 02:55:04 +08:00
|
|
|
return !MCDwarfFiles[FileNumber].Name.empty();
|
2010-08-25 04:32:42 +08:00
|
|
|
}
|
2012-01-27 07:20:11 +08:00
|
|
|
|
2015-05-22 00:52:32 +08:00
|
|
|
/// Remove empty sections from SectionStartEndSyms, to avoid generating
|
2014-06-19 23:52:37 +08:00
|
|
|
/// useless debug info for them.
|
|
|
|
void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
|
2015-06-01 02:49:28 +08:00
|
|
|
SectionsForRanges.remove_if(
|
|
|
|
[&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
|
2014-06-19 23:52:37 +08:00
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
CodeViewContext &MCContext::getCVContext() {
|
|
|
|
if (!CVContext.get())
|
|
|
|
CVContext.reset(new CodeViewContext);
|
|
|
|
return *CVContext.get();
|
|
|
|
}
|
|
|
|
|
2015-11-17 17:58:07 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Error Reporting
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void MCContext::reportError(SMLoc Loc, const Twine &Msg) {
|
|
|
|
HadError = true;
|
|
|
|
|
[Assembler] Enable nicer diagnostics for inline assembly.
Fixed test.
Summary:
Enables source location in diagnostic messages from the backend. This
is after parsing, during finalization. This requires the SourceMgr, the
inline assembly string buffer, and DiagInfo to still be alive after
EmitInlineAsm returns.
This patch creates a single SourceMgr for inline assembly inside the
AsmPrinter. MCContext gets a pointer to this SourceMgr. Using one
SourceMgr per call to EmitInlineAsm would make it difficult for
MCContext to figure out in which SourceMgr the SMLoc is located, while a
single SourceMgr can figure it out if it has multiple buffers.
The Str argument to EmitInlineAsm is copied into a buffer and owned by
the inline asm SourceMgr. This ensures that DiagHandlers won't print
garbage. (Clang emits a "note: instantiated into assembly here", which
refers to this string.)
The AsmParser gets destroyed before finalization, which means that the
DiagHandlers the AsmParser installs into the SourceMgr will be stale.
Restore the saved DiagHandlers.
Since now we're using just one SourceMgr for multiple inline asm
strings, we need to tell the AsmParser which buffer it needs to parse
currently. Hand a buffer id -- returned from SourceMgr::
AddNewSourceBuffer -- to the AsmParser.
Reviewers: rnk, grosbach, compnerd, rengolin, rovka, anemet
Reviewed By: rnk
Subscribers: llvm-commits
Differential Revision: https://reviews.llvm.org/D29441
llvm-svn: 294458
2017-02-08 22:48:05 +08:00
|
|
|
// If we have a source manager use it. Otherwise, try using the inline source
|
|
|
|
// manager.
|
|
|
|
// If that fails, use the generic report_fatal_error().
|
|
|
|
if (SrcMgr)
|
|
|
|
SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
|
|
|
|
else if (InlineSrcMgr)
|
|
|
|
InlineSrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
|
|
|
|
else
|
2014-03-15 06:41:58 +08:00
|
|
|
report_fatal_error(Msg, false);
|
2015-11-17 17:58:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) {
|
|
|
|
reportError(Loc, Msg);
|
2012-01-27 07:20:11 +08:00
|
|
|
|
|
|
|
// If we reached here, we are failing ungracefully. Run the interrupt handlers
|
|
|
|
// to make sure any special cleanups get done, in particular that we remove
|
|
|
|
// files registered with RemoveFileOnSignal.
|
|
|
|
sys::RunInterruptHandlers();
|
|
|
|
exit(1);
|
|
|
|
}
|