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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/MC/MCContext.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2015-05-26 08:32:28 +08:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2010-03-12 06:56:10 +08:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
|
|
|
#include "llvm/MC/MCLabel.h"
|
2011-07-20 13:58:47 +08:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2011-07-19 04:57:22 +08:00
|
|
|
#include "llvm/MC/MCRegisterInfo.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"
|
2015-03-04 09:24:11 +08:00
|
|
|
#include "llvm/MC/MCStreamer.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"
|
2011-01-23 12:28:49 +08:00
|
|
|
#include "llvm/Support/ELF.h"
|
2012-01-27 07:20:11 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-06-15 04:26:58 +08:00
|
|
|
#include "llvm/Support/FileSystem.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"
|
2013-10-23 07:41:52 +08:00
|
|
|
#include <map>
|
|
|
|
|
2009-06-24 06:01:43 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
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)
|
|
|
|
: SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
|
2015-03-18 04:07:06 +08:00
|
|
|
Symbols(Allocator), UsedNames(Allocator),
|
2014-03-28 04:45:58 +08:00
|
|
|
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
|
2014-05-02 03:55:34 +08:00
|
|
|
GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
|
2014-03-28 04:45:58 +08:00
|
|
|
AllowTemporaryLabels(true), DwarfCompileUnitID(0),
|
|
|
|
AutoReset(DoAutoReset) {
|
2012-12-13 06:59:46 +08:00
|
|
|
|
2014-06-13 10:24:39 +08:00
|
|
|
std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
|
2013-12-10 12:39:09 +08:00
|
|
|
if (EC)
|
|
|
|
CompilationDir.clear();
|
2013-06-15 04:26:58 +08:00
|
|
|
|
2010-06-29 05:45:58 +08:00
|
|
|
SecureLogFile = getenv("AS_SECURE_LOG_FILE");
|
2014-04-13 12:57:38 +08:00
|
|
|
SecureLog = nullptr;
|
2010-06-29 05:45:58 +08:00
|
|
|
SecureLogUsed = false;
|
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.
|
2013-12-10 12:39:05 +08:00
|
|
|
|
2012-12-07 06:12:44 +08:00
|
|
|
// If the stream for the .secure_log_unique directive was created free it.
|
2015-05-19 02:43:23 +08:00
|
|
|
delete (raw_ostream *)SecureLog;
|
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
|
|
|
|
for (auto &I : ELFUniquingMap)
|
|
|
|
I.second->~MCSectionELF();
|
|
|
|
for (auto &I : COFFUniquingMap)
|
|
|
|
I.second->~MCSectionCOFF();
|
|
|
|
for (auto &I : MachOUniquingMap)
|
|
|
|
I.second->~MCSectionMachO();
|
|
|
|
|
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
|
|
|
|
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;
|
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-06-02 08:25:12 +08:00
|
|
|
MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
|
|
|
|
MCSymbolELF *&Sym = SectionSymbols[&Section];
|
2014-10-17 09:48:58 +08:00
|
|
|
if (Sym)
|
|
|
|
return Sym;
|
|
|
|
|
|
|
|
StringRef Name = Section.getSectionName();
|
|
|
|
|
2014-11-19 13:49:42 +08:00
|
|
|
MCSymbol *&OldSym = Symbols[Name];
|
2014-10-17 09:48:58 +08:00
|
|
|
if (OldSym && OldSym->isUndefined()) {
|
2015-06-02 08:25:12 +08:00
|
|
|
Sym = cast<MCSymbolELF>(OldSym);
|
|
|
|
return Sym;
|
2014-10-17 09:48:58 +08:00
|
|
|
}
|
|
|
|
|
2014-11-19 13:49:42 +08:00
|
|
|
auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
|
2015-06-10 02:36:13 +08:00
|
|
|
Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
|
2014-10-17 09:48:58 +08:00
|
|
|
|
2014-11-19 13:49:42 +08:00
|
|
|
if (!OldSym)
|
|
|
|
OldSym = Sym;
|
2014-10-17 09:48:58 +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) {
|
|
|
|
switch (MOFI->getObjectFileType()) {
|
|
|
|
case MCObjectFileInfo::IsCOFF:
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
|
2015-06-09 01:17:12 +08:00
|
|
|
case MCObjectFileInfo::IsELF:
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbolELF(Name, IsTemporary);
|
2015-06-09 01:17:12 +08:00
|
|
|
case MCObjectFileInfo::IsMachO:
|
2015-06-10 02:36:13 +08:00
|
|
|
return new (Name, *this) MCSymbolMachO(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);
|
|
|
|
|
2015-06-03 06:52:13 +08:00
|
|
|
// Determine whether this is an user writter assembler temporary or normal
|
|
|
|
// 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];
|
|
|
|
for (;;) {
|
|
|
|
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));
|
|
|
|
if (NameEntry.second) {
|
|
|
|
// Ok, we found a name. 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
|
|
|
}
|
|
|
|
|
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-11-26 12:16:08 +08:00
|
|
|
|
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.
|
2010-04-09 05:26:26 +08:00
|
|
|
return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
|
2015-03-11 06:00:25 +08:00
|
|
|
Reserved2, Kind, Begin);
|
2010-04-09 04:30:37 +08:00
|
|
|
}
|
2010-04-09 05:26:26 +08:00
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
|
2014-04-11 05:53:53 +08:00
|
|
|
StringRef GroupName;
|
|
|
|
if (const MCSymbol *Group = Section->getGroup())
|
|
|
|
GroupName = Group->getName();
|
|
|
|
|
2015-04-05 02:02:01 +08:00
|
|
|
unsigned UniqueID = Section->getUniqueID();
|
|
|
|
ELFUniquingMap.erase(
|
|
|
|
ELFSectionKey{Section->getSectionName(), GroupName, UniqueID});
|
|
|
|
auto I = ELFUniquingMap.insert(std::make_pair(
|
|
|
|
ELFSectionKey{Name, GroupName, UniqueID},
|
2015-05-19 02:43:23 +08:00
|
|
|
Section))
|
|
|
|
.first;
|
2015-03-28 05:34:24 +08:00
|
|
|
StringRef CachedName = I->first.SectionName;
|
2015-05-19 02:43:23 +08:00
|
|
|
const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
|
2014-04-11 05:53:53 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
|
|
|
|
unsigned Flags, unsigned EntrySize,
|
2015-06-03 05:30:13 +08:00
|
|
|
const MCSymbolELF *Group,
|
2015-05-22 03:20:38 +08:00
|
|
|
const MCSectionELF *Associated) {
|
2015-03-30 21:39:16 +08:00
|
|
|
StringMap<bool>::iterator I;
|
|
|
|
bool Inserted;
|
|
|
|
std::tie(I, Inserted) = ELFRelSecNames.insert(std::make_pair(Name, true));
|
|
|
|
|
|
|
|
return new (*this)
|
|
|
|
MCSectionELF(I->getKey(), Type, Flags, SectionKind::getReadOnly(),
|
2015-04-06 11:09:30 +08:00
|
|
|
EntrySize, Group, true, nullptr, Associated);
|
2015-03-30 21:39:16 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
|
|
|
|
unsigned Flags, unsigned EntrySize,
|
|
|
|
StringRef Group, unsigned UniqueID,
|
|
|
|
const char *BeginSymName) {
|
2015-06-03 05:30:13 +08:00
|
|
|
MCSymbolELF *GroupSym = nullptr;
|
2015-04-06 12:25:18 +08:00
|
|
|
if (!Group.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,
|
|
|
|
BeginSymName, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
|
|
|
|
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,
|
|
|
|
const char *BeginSymName,
|
|
|
|
const MCSectionELF *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(
|
2015-04-05 02:02:01 +08:00
|
|
|
std::make_pair(ELFSectionKey{Section, 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;
|
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
|
|
|
Kind = SectionKind::getText();
|
|
|
|
else
|
|
|
|
Kind = SectionKind::getReadOnly();
|
|
|
|
|
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-04-06 11:09:30 +08:00
|
|
|
MCSectionELF *Result =
|
|
|
|
new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize,
|
2015-04-06 12:25:18 +08:00
|
|
|
GroupSym, UniqueID, Begin, 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) {
|
2015-03-11 06:00:25 +08:00
|
|
|
MCSectionELF *Result = new (*this)
|
|
|
|
MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
|
2015-04-29 05:07:28 +08:00
|
|
|
Group, ~0, nullptr, nullptr);
|
2010-11-12 02:13:52 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
|
|
|
|
unsigned Characteristics,
|
|
|
|
SectionKind Kind,
|
|
|
|
StringRef COMDATSymName, int Selection,
|
|
|
|
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
|
|
|
|
2015-03-30 21:59:06 +08:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2015-03-28 05:34:24 +08:00
|
|
|
COFFSectionKey T{Section, COMDATSymName, Selection};
|
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-03-11 06:00:25 +08:00
|
|
|
MCSectionCOFF *Result = new (*this) MCSectionCOFF(
|
|
|
|
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) {
|
2015-03-11 06:00:25 +08:00
|
|
|
return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
|
2013-11-20 03:52:52 +08:00
|
|
|
}
|
|
|
|
|
2015-05-22 03:20:38 +08:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
|
2015-03-28 05:34:24 +08:00
|
|
|
COFFSectionKey T{Section, "", 0};
|
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,
|
|
|
|
const MCSymbol *KeySym) {
|
2014-09-05 01:42:03 +08:00
|
|
|
// Return the normal section if we don't have to be associative.
|
|
|
|
if (!KeySym)
|
|
|
|
return Sec;
|
|
|
|
|
|
|
|
// Make an associative section with the same name and kind as the normal
|
|
|
|
// section.
|
|
|
|
unsigned Characteristics =
|
|
|
|
Sec->getCharacteristics() | COFF::IMAGE_SCN_LNK_COMDAT;
|
|
|
|
return getCOFFSection(Sec->getSectionName(), Characteristics, Sec->getKind(),
|
|
|
|
KeySym->getName(),
|
|
|
|
COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE);
|
|
|
|
}
|
|
|
|
|
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,
|
2013-03-07 09:42:00 +08:00
|
|
|
unsigned FileNumber, unsigned CUID) {
|
2014-03-14 05:59:51 +08:00
|
|
|
MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
|
2014-03-14 03:15:04 +08:00
|
|
|
return Table.getFile(Directory, FileName, 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
|
|
|
}
|
|
|
|
|
2015-05-19 02:43:14 +08:00
|
|
|
void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) const {
|
2012-01-27 07:20:11 +08:00
|
|
|
// If we have a source manager and a location, use it. Otherwise just
|
|
|
|
// use the generic report_fatal_error().
|
|
|
|
if (!SrcMgr || Loc == SMLoc())
|
2014-03-15 06:41:58 +08:00
|
|
|
report_fatal_error(Msg, false);
|
2012-01-27 07:20:11 +08:00
|
|
|
|
|
|
|
// Use the source manager to print the message.
|
|
|
|
SrcMgr->PrintMessage(Loc, SourceMgr::DK_Error, Msg);
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|