2009-06-24 01:39:15 +02:00
|
|
|
//===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
|
2009-06-24 00:01:43 +02: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-03 17:50:05 +01:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2015-05-26 02:32:28 +02:00
|
|
|
#include "llvm/MC/MCAssembler.h"
|
2010-03-11 23:56:10 +01:00
|
|
|
#include "llvm/MC/MCAsmInfo.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/MC/MCDwarf.h"
|
|
|
|
#include "llvm/MC/MCLabel.h"
|
2011-07-20 07:58:47 +02:00
|
|
|
#include "llvm/MC/MCObjectFileInfo.h"
|
2011-07-18 22:57:22 +02:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2010-05-07 19:17:41 +02:00
|
|
|
#include "llvm/MC/MCSectionCOFF.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
|
|
|
#include "llvm/MC/MCSectionMachO.h"
|
2015-03-04 02:24:11 +01:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2015-06-08 19:17:12 +02:00
|
|
|
#include "llvm/MC/MCSymbolCOFF.h"
|
2015-06-02 02:25:12 +02:00
|
|
|
#include "llvm/MC/MCSymbolELF.h"
|
2015-06-08 19:17:23 +02:00
|
|
|
#include "llvm/MC/MCSymbolMachO.h"
|
2011-01-23 05:28:49 +01:00
|
|
|
#include "llvm/Support/ELF.h"
|
2012-01-27 00:20:11 +01:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-06-14 22:26:58 +02:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2012-12-18 01:31:01 +01:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2012-01-27 00:20:11 +01:00
|
|
|
#include "llvm/Support/Signals.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2013-10-23 01:41:52 +02:00
|
|
|
#include <map>
|
|
|
|
|
2009-06-24 00:01:43 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2013-06-18 09:20:20 +02:00
|
|
|
MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
|
2012-12-06 23:12:44 +01:00
|
|
|
const MCObjectFileInfo *mofi, const SourceMgr *mgr,
|
2014-03-27 21:45:58 +01:00
|
|
|
bool DoAutoReset)
|
|
|
|
: SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
|
2015-03-17 21:07:06 +01:00
|
|
|
Symbols(Allocator), UsedNames(Allocator),
|
2014-03-27 21:45:58 +01:00
|
|
|
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
|
2014-05-01 21:55:34 +02:00
|
|
|
GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
|
2014-03-27 21:45:58 +01:00
|
|
|
AllowTemporaryLabels(true), DwarfCompileUnitID(0),
|
|
|
|
AutoReset(DoAutoReset) {
|
2012-12-12 23:59:46 +01:00
|
|
|
|
2014-06-13 04:24:39 +02:00
|
|
|
std::error_code EC = llvm::sys::fs::current_path(CompilationDir);
|
2013-12-10 05:39:09 +01:00
|
|
|
if (EC)
|
|
|
|
CompilationDir.clear();
|
2013-06-14 22:26:58 +02:00
|
|
|
|
2010-06-28 23:45:58 +02:00
|
|
|
SecureLogFile = getenv("AS_SECURE_LOG_FILE");
|
2014-04-13 06:57:38 +02:00
|
|
|
SecureLog = nullptr;
|
2010-06-28 23:45:58 +02:00
|
|
|
SecureLogUsed = false;
|
2012-12-18 01:31:01 +01:00
|
|
|
|
2014-07-06 12:33:31 +02:00
|
|
|
if (SrcMgr && SrcMgr->getNumBuffers())
|
|
|
|
MainFileName =
|
|
|
|
SrcMgr->getMemoryBuffer(SrcMgr->getMainFileID())->getBufferIdentifier();
|
2009-06-24 00:01:43 +02:00
|
|
|
}
|
|
|
|
|
2012-12-06 03:00:13 +01:00
|
|
|
MCContext::~MCContext() {
|
2012-12-12 23:59:46 +01:00
|
|
|
if (AutoReset)
|
|
|
|
reset();
|
2012-12-06 23:12:44 +01:00
|
|
|
|
2012-12-06 03:00:13 +01:00
|
|
|
// NOTE: The symbols are all allocated out of a bump pointer allocator,
|
|
|
|
// we don't need to free them here.
|
2013-12-10 05:39:05 +01:00
|
|
|
|
2012-12-06 23:12:44 +01:00
|
|
|
// If the stream for the .secure_log_unique directive was created free it.
|
2015-05-18 20:43:23 +02:00
|
|
|
delete (raw_ostream *)SecureLog;
|
2012-12-06 23:12:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Module Lifetime Management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-12 23:59:46 +01:00
|
|
|
void MCContext::reset() {
|
2015-05-26 03:52:19 +02: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-06 23:12:44 +01:00
|
|
|
UsedNames.clear();
|
|
|
|
Symbols.clear();
|
|
|
|
Allocator.Reset();
|
|
|
|
Instances.clear();
|
2014-09-17 11:25:36 +02:00
|
|
|
CompilationDir.clear();
|
|
|
|
MainFileName.clear();
|
2014-03-13 22:59:51 +01:00
|
|
|
MCDwarfLineTablesCUMap.clear();
|
2015-05-21 18:52:32 +02:00
|
|
|
SectionsForRanges.clear();
|
2012-12-06 23:12:44 +01:00
|
|
|
MCGenDwarfLabelEntries.clear();
|
|
|
|
DwarfDebugFlags = StringRef();
|
2013-02-20 01:10:29 +01:00
|
|
|
DwarfCompileUnitID = 0;
|
2015-05-18 20:43:23 +02:00
|
|
|
CurrentDwarfLoc = MCDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0);
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2014-04-11 01:55:11 +02:00
|
|
|
MachOUniquingMap.clear();
|
|
|
|
ELFUniquingMap.clear();
|
|
|
|
COFFUniquingMap.clear();
|
2012-12-12 23:59:46 +01:00
|
|
|
|
2015-03-17 21:07:06 +01:00
|
|
|
NextID.clear();
|
2012-12-12 23:59:46 +01:00
|
|
|
AllowTemporaryLabels = true;
|
|
|
|
DwarfLocSeen = false;
|
|
|
|
GenDwarfForAssembly = false;
|
|
|
|
GenDwarfFileNumber = 0;
|
2009-06-24 00:01:43 +02:00
|
|
|
}
|
|
|
|
|
2010-04-08 22:30:37 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Symbol Manipulation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
MCSymbol *MCContext::getOrCreateSymbol(const Twine &Name) {
|
2015-03-17 10:51:17 +01:00
|
|
|
SmallString<128> NameSV;
|
|
|
|
StringRef NameRef = Name.toStringRef(NameSV);
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2015-03-17 10:51:17 +01:00
|
|
|
assert(!NameRef.empty() && "Normal symbols cannot be unnamed!");
|
2010-12-01 21:46:11 +01:00
|
|
|
|
2015-03-17 10:51:17 +01:00
|
|
|
MCSymbol *&Sym = Symbols[NameRef];
|
2014-11-19 06:49:42 +01:00
|
|
|
if (!Sym)
|
2015-06-23 13:31:32 +02:00
|
|
|
Sym = createSymbol(NameRef, false, false);
|
2010-12-01 21:46:11 +01:00
|
|
|
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-06-02 02:25:12 +02:00
|
|
|
MCSymbolELF *MCContext::getOrCreateSectionSymbol(const MCSectionELF &Section) {
|
|
|
|
MCSymbolELF *&Sym = SectionSymbols[&Section];
|
2014-10-17 03:48:58 +02:00
|
|
|
if (Sym)
|
|
|
|
return Sym;
|
|
|
|
|
|
|
|
StringRef Name = Section.getSectionName();
|
|
|
|
|
2014-11-19 06:49:42 +01:00
|
|
|
MCSymbol *&OldSym = Symbols[Name];
|
2014-10-17 03:48:58 +02:00
|
|
|
if (OldSym && OldSym->isUndefined()) {
|
2015-06-02 02:25:12 +02:00
|
|
|
Sym = cast<MCSymbolELF>(OldSym);
|
|
|
|
return Sym;
|
2014-10-17 03:48:58 +02:00
|
|
|
}
|
|
|
|
|
2014-11-19 06:49:42 +01:00
|
|
|
auto NameIter = UsedNames.insert(std::make_pair(Name, true)).first;
|
2015-06-09 20:36:13 +02:00
|
|
|
Sym = new (&*NameIter, *this) MCSymbolELF(&*NameIter, /*isTemporary*/ false);
|
2014-10-17 03:48:58 +02:00
|
|
|
|
2014-11-19 06:49:42 +01:00
|
|
|
if (!OldSym)
|
|
|
|
OldSym = Sym;
|
2014-10-17 03:48:58 +02:00
|
|
|
|
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-03-05 19:26:34 +01:00
|
|
|
MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
|
|
|
|
unsigned Idx) {
|
2015-05-18 20:43:14 +02:00
|
|
|
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
|
2015-03-05 19:26:34 +01:00
|
|
|
"$frame_escape_" + Twine(Idx));
|
2015-01-13 01:48:10 +01:00
|
|
|
}
|
|
|
|
|
2015-04-01 00:35:44 +02:00
|
|
|
MCSymbol *MCContext::getOrCreateParentFrameOffsetSymbol(StringRef FuncName) {
|
2015-05-18 20:43:14 +02:00
|
|
|
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + FuncName +
|
2015-04-01 00:35:44 +02:00
|
|
|
"$parent_frame_offset");
|
|
|
|
}
|
|
|
|
|
2015-05-21 01:08:04 +02:00
|
|
|
MCSymbol *MCContext::getOrCreateLSDASymbol(StringRef FuncName) {
|
|
|
|
return getOrCreateSymbol(Twine(MAI->getPrivateGlobalPrefix()) + "__ehtable$" +
|
|
|
|
FuncName);
|
|
|
|
}
|
|
|
|
|
2015-06-02 02:25:12 +02:00
|
|
|
MCSymbol *MCContext::createSymbolImpl(const StringMapEntry<bool> *Name,
|
|
|
|
bool IsTemporary) {
|
2015-06-08 19:17:12 +02:00
|
|
|
if (MOFI) {
|
|
|
|
switch (MOFI->getObjectFileType()) {
|
|
|
|
case MCObjectFileInfo::IsCOFF:
|
2015-06-09 20:36:13 +02:00
|
|
|
return new (Name, *this) MCSymbolCOFF(Name, IsTemporary);
|
2015-06-08 19:17:12 +02:00
|
|
|
case MCObjectFileInfo::IsELF:
|
2015-06-09 20:36:13 +02:00
|
|
|
return new (Name, *this) MCSymbolELF(Name, IsTemporary);
|
2015-06-08 19:17:12 +02:00
|
|
|
case MCObjectFileInfo::IsMachO:
|
2015-06-09 20:36:13 +02:00
|
|
|
return new (Name, *this) MCSymbolMachO(Name, IsTemporary);
|
2015-06-08 19:17:12 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-09 20:36:13 +02:00
|
|
|
return new (Name, *this) MCSymbol(MCSymbol::SymbolKindUnset, Name,
|
|
|
|
IsTemporary);
|
2015-06-02 02:25:12 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:31:32 +02:00
|
|
|
MCSymbol *MCContext::createSymbol(StringRef Name, bool AlwaysAddSuffix,
|
|
|
|
bool CanBeUnnamed) {
|
|
|
|
if (CanBeUnnamed && !UseNamesOnTempLabels)
|
|
|
|
return createSymbolImpl(nullptr, true);
|
|
|
|
|
2015-06-03 00:52:13 +02:00
|
|
|
// Determine whether this is an user writter assembler temporary or normal
|
|
|
|
// label, if used.
|
2015-06-23 13:31:32 +02:00
|
|
|
bool IsTemporary = CanBeUnnamed;
|
|
|
|
if (AllowTemporaryLabels && !IsTemporary)
|
2015-03-17 21:07:06 +01:00
|
|
|
IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
|
2010-12-01 21:46:11 +01:00
|
|
|
|
2015-03-17 21:07:06 +01:00
|
|
|
SmallString<128> NewName = Name;
|
|
|
|
bool AddSuffix = AlwaysAddSuffix;
|
|
|
|
unsigned &NextUniqueID = NextID[Name];
|
|
|
|
for (;;) {
|
|
|
|
if (AddSuffix) {
|
2011-04-09 13:26:27 +02:00
|
|
|
NewName.resize(Name.size());
|
|
|
|
raw_svector_ostream(NewName) << NextUniqueID++;
|
2015-03-17 21:07:06 +01: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 02:25:12 +02:00
|
|
|
return createSymbolImpl(&*NameEntry.first, IsTemporary);
|
2015-03-17 21:07:06 +01:00
|
|
|
}
|
|
|
|
assert(IsTemporary && "Cannot rename non-temporary symbols");
|
|
|
|
AddSuffix = true;
|
2010-12-01 21:46:11 +01:00
|
|
|
}
|
2015-03-17 21:07:06 +01:00
|
|
|
llvm_unreachable("Infinite loop");
|
2009-06-24 06:31:49 +02:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:31:32 +02:00
|
|
|
MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix,
|
|
|
|
bool CanBeUnnamed) {
|
2015-02-27 19:18:39 +01:00
|
|
|
SmallString<128> NameSV;
|
|
|
|
raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
|
2015-06-23 13:31:32 +02:00
|
|
|
return createSymbol(NameSV, AlwaysAddSuffix, CanBeUnnamed);
|
2015-02-27 19:18:39 +01:00
|
|
|
}
|
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
MCSymbol *MCContext::createLinkerPrivateTempSymbol() {
|
2014-06-27 00:52:05 +02:00
|
|
|
SmallString<128> NameSV;
|
2015-03-17 21:07:06 +01:00
|
|
|
raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
|
2015-06-23 13:31:32 +02:00
|
|
|
return createSymbol(NameSV, true, false);
|
2014-03-29 08:05:06 +01:00
|
|
|
}
|
|
|
|
|
2015-06-23 13:31:32 +02:00
|
|
|
MCSymbol *MCContext::createTempSymbol(bool CanBeUnnamed) {
|
|
|
|
return createTempSymbol("tmp", true, CanBeUnnamed);
|
2010-03-10 02:29:27 +01:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:09:26 +01:00
|
|
|
unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
|
2010-05-18 14:15:34 +02:00
|
|
|
MCLabel *&Label = Instances[LocalLabelVal];
|
|
|
|
if (!Label)
|
|
|
|
Label = new (*this) MCLabel(0);
|
|
|
|
return Label->incInstance();
|
2010-05-18 01:08:19 +02:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:09:26 +01:00
|
|
|
unsigned MCContext::GetInstance(unsigned LocalLabelVal) {
|
2010-05-18 14:15:34 +02:00
|
|
|
MCLabel *&Label = Instances[LocalLabelVal];
|
|
|
|
if (!Label)
|
|
|
|
Label = new (*this) MCLabel(0);
|
|
|
|
return Label->getInstance();
|
2010-05-18 01:08:19 +02:00
|
|
|
}
|
|
|
|
|
2014-03-13 19:09:26 +01:00
|
|
|
MCSymbol *MCContext::getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
|
|
|
|
unsigned Instance) {
|
|
|
|
MCSymbol *&Sym = LocalSymbols[std::make_pair(LocalLabelVal, Instance)];
|
|
|
|
if (!Sym)
|
2015-06-23 13:31:32 +02:00
|
|
|
Sym = createTempSymbol(false);
|
2014-03-13 19:09:26 +01:00
|
|
|
return Sym;
|
|
|
|
}
|
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
MCSymbol *MCContext::createDirectionalLocalSymbol(unsigned LocalLabelVal) {
|
2014-03-13 19:09:26 +01:00
|
|
|
unsigned Instance = NextInstance(LocalLabelVal);
|
|
|
|
return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
|
2010-05-18 01:08:19 +02:00
|
|
|
}
|
2014-03-13 19:09:26 +01:00
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
MCSymbol *MCContext::getDirectionalLocalSymbol(unsigned LocalLabelVal,
|
2014-03-13 19:09:26 +01:00
|
|
|
bool Before) {
|
|
|
|
unsigned Instance = GetInstance(LocalLabelVal);
|
|
|
|
if (!Before)
|
|
|
|
++Instance;
|
|
|
|
return getOrCreateDirectionalLocalSymbol(LocalLabelVal, Instance);
|
2010-05-18 01:08:19 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
MCSymbol *MCContext::lookupSymbol(const Twine &Name) const {
|
2012-09-18 19:10:37 +02:00
|
|
|
SmallString<128> NameSV;
|
2015-03-17 19:55:30 +01:00
|
|
|
StringRef NameRef = Name.toStringRef(NameSV);
|
|
|
|
return Symbols.lookup(NameRef);
|
2012-09-18 19:10:37 +02:00
|
|
|
}
|
|
|
|
|
2010-04-08 22:30:37 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Section Management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionMachO *MCContext::getMachOSection(StringRef Segment, StringRef Section,
|
|
|
|
unsigned TypeAndAttributes,
|
|
|
|
unsigned Reserved2, SectionKind Kind,
|
|
|
|
const char *BeginSymName) {
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2010-04-08 22:30:37 +02: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 05:16:08 +01:00
|
|
|
|
2010-04-08 22:30:37 +02:00
|
|
|
// Form the name to look up.
|
|
|
|
SmallString<64> Name;
|
|
|
|
Name += Segment;
|
|
|
|
Name.push_back(',');
|
|
|
|
Name += Section;
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2010-04-08 22:30:37 +02:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionMachO *&Entry = MachOUniquingMap[Name];
|
2015-03-10 22:16:18 +01:00
|
|
|
if (Entry)
|
|
|
|
return Entry;
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2015-03-10 23:00:25 +01:00
|
|
|
MCSymbol *Begin = nullptr;
|
|
|
|
if (BeginSymName)
|
2015-03-17 21:07:06 +01:00
|
|
|
Begin = createTempSymbol(BeginSymName, false);
|
2015-03-10 23:00:25 +01:00
|
|
|
|
2010-04-08 22:30:37 +02:00
|
|
|
// Otherwise, return a new section.
|
2010-04-08 23:26:26 +02:00
|
|
|
return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
|
2015-03-10 23:00:25 +01:00
|
|
|
Reserved2, Kind, Begin);
|
2010-04-08 22:30:37 +02:00
|
|
|
}
|
2010-04-08 23:26:26 +02:00
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
void MCContext::renameELFSection(MCSectionELF *Section, StringRef Name) {
|
2014-04-10 23:53:53 +02:00
|
|
|
StringRef GroupName;
|
|
|
|
if (const MCSymbol *Group = Section->getGroup())
|
|
|
|
GroupName = Group->getName();
|
|
|
|
|
2015-04-04 20:02:01 +02: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-18 20:43:23 +02:00
|
|
|
Section))
|
|
|
|
.first;
|
2015-03-27 22:34:24 +01:00
|
|
|
StringRef CachedName = I->first.SectionName;
|
2015-05-18 20:43:23 +02:00
|
|
|
const_cast<MCSectionELF *>(Section)->setSectionName(CachedName);
|
2014-04-10 23:53:53 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionELF *MCContext::createELFRelSection(StringRef Name, unsigned Type,
|
|
|
|
unsigned Flags, unsigned EntrySize,
|
2015-06-02 23:30:13 +02:00
|
|
|
const MCSymbolELF *Group,
|
2015-05-21 21:20:38 +02:00
|
|
|
const MCSectionELF *Associated) {
|
2015-03-30 15:39:16 +02: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 05:09:30 +02:00
|
|
|
EntrySize, Group, true, nullptr, Associated);
|
2015-03-30 15:39:16 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
|
|
|
|
unsigned Flags, unsigned EntrySize,
|
|
|
|
StringRef Group, unsigned UniqueID,
|
|
|
|
const char *BeginSymName) {
|
2015-06-02 23:30:13 +02:00
|
|
|
MCSymbolELF *GroupSym = nullptr;
|
2015-04-06 06:25:18 +02:00
|
|
|
if (!Group.empty())
|
2015-06-02 23:30:13 +02:00
|
|
|
GroupSym = cast<MCSymbolELF>(getOrCreateSymbol(Group));
|
2015-03-30 15:59:06 +02:00
|
|
|
|
2015-04-06 06:25:18 +02:00
|
|
|
return getELFSection(Section, Type, Flags, EntrySize, GroupSym, UniqueID,
|
|
|
|
BeginSymName, nullptr);
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
|
|
|
|
unsigned Flags, unsigned EntrySize,
|
2015-06-02 23:30:13 +02:00
|
|
|
const MCSymbolELF *GroupSym,
|
2015-05-21 21:20:38 +02:00
|
|
|
unsigned UniqueID,
|
|
|
|
const char *BeginSymName,
|
|
|
|
const MCSectionELF *Associated) {
|
2015-04-06 06:25:18 +02:00
|
|
|
StringRef Group = "";
|
|
|
|
if (GroupSym)
|
|
|
|
Group = GroupSym->getName();
|
2010-04-08 23:26:26 +02:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2014-04-11 01:55:11 +02:00
|
|
|
auto IterBool = ELFUniquingMap.insert(
|
2015-04-04 20:02:01 +02:00
|
|
|
std::make_pair(ELFSectionKey{Section, Group, UniqueID}, nullptr));
|
2014-04-11 01:55:11 +02:00
|
|
|
auto &Entry = *IterBool.first;
|
2015-04-04 20:02:01 +02:00
|
|
|
if (!IterBool.second)
|
2015-02-17 21:48:01 +01:00
|
|
|
return Entry.second;
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2015-03-27 22:34:24 +01:00
|
|
|
StringRef CachedName = Entry.first.SectionName;
|
2015-01-29 18:33:21 +01:00
|
|
|
|
|
|
|
SectionKind Kind;
|
|
|
|
if (Flags & ELF::SHF_EXECINSTR)
|
|
|
|
Kind = SectionKind::getText();
|
|
|
|
else
|
|
|
|
Kind = SectionKind::getReadOnly();
|
|
|
|
|
2015-03-10 23:00:25 +01:00
|
|
|
MCSymbol *Begin = nullptr;
|
|
|
|
if (BeginSymName)
|
2015-03-17 21:07:06 +01:00
|
|
|
Begin = createTempSymbol(BeginSymName, false);
|
2015-03-10 23:00:25 +01:00
|
|
|
|
2015-04-06 05:09:30 +02:00
|
|
|
MCSectionELF *Result =
|
|
|
|
new (*this) MCSectionELF(CachedName, Type, Flags, Kind, EntrySize,
|
2015-04-06 06:25:18 +02:00
|
|
|
GroupSym, UniqueID, Begin, Associated);
|
2015-04-04 20:02:01 +02:00
|
|
|
Entry.second = Result;
|
2010-04-08 23:26:26 +02:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-06-02 23:30:13 +02:00
|
|
|
MCSectionELF *MCContext::createELFGroupSection(const MCSymbolELF *Group) {
|
2015-03-10 23:00:25 +01:00
|
|
|
MCSectionELF *Result = new (*this)
|
|
|
|
MCSectionELF(".group", ELF::SHT_GROUP, 0, SectionKind::getReadOnly(), 4,
|
2015-04-28 23:07:28 +02:00
|
|
|
Group, ~0, nullptr, nullptr);
|
2010-11-11 19:13:52 +01:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
|
|
|
|
unsigned Characteristics,
|
|
|
|
SectionKind Kind,
|
|
|
|
StringRef COMDATSymName, int Selection,
|
|
|
|
const char *BeginSymName) {
|
2015-03-30 15:59:06 +02:00
|
|
|
MCSymbol *COMDATSymbol = nullptr;
|
|
|
|
if (!COMDATSymName.empty()) {
|
2015-05-18 20:43:14 +02:00
|
|
|
COMDATSymbol = getOrCreateSymbol(COMDATSymName);
|
2015-03-30 15:59:06 +02:00
|
|
|
COMDATSymName = COMDATSymbol->getName();
|
|
|
|
}
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2015-03-30 15:59:06 +02:00
|
|
|
// Do the lookup, if we have a hit, return it.
|
2015-03-27 22:34:24 +01:00
|
|
|
COFFSectionKey T{Section, COMDATSymName, Selection};
|
2014-06-27 19:19:44 +02:00
|
|
|
auto IterBool = COFFUniquingMap.insert(std::make_pair(T, nullptr));
|
2014-04-11 01:55:11 +02:00
|
|
|
auto Iter = IterBool.first;
|
|
|
|
if (!IterBool.second)
|
2013-11-19 20:52:52 +01:00
|
|
|
return Iter->second;
|
|
|
|
|
2015-03-10 23:00:25 +01:00
|
|
|
MCSymbol *Begin = nullptr;
|
|
|
|
if (BeginSymName)
|
2015-03-17 21:07:06 +01:00
|
|
|
Begin = createTempSymbol(BeginSymName, false);
|
2015-03-10 23:00:25 +01:00
|
|
|
|
2015-03-27 22:34:24 +01:00
|
|
|
StringRef CachedName = Iter->first.SectionName;
|
2015-03-10 23:00:25 +01:00
|
|
|
MCSectionCOFF *Result = new (*this) MCSectionCOFF(
|
|
|
|
CachedName, Characteristics, COMDATSymbol, Selection, Kind, Begin);
|
2010-11-26 05:16:08 +01:00
|
|
|
|
2013-11-19 20:52:52 +01:00
|
|
|
Iter->second = Result;
|
2010-05-07 19:17:41 +02:00
|
|
|
return Result;
|
|
|
|
}
|
2010-07-28 22:55:35 +02:00
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section,
|
|
|
|
unsigned Characteristics,
|
|
|
|
SectionKind Kind,
|
|
|
|
const char *BeginSymName) {
|
2015-03-10 23:00:25 +01:00
|
|
|
return getCOFFSection(Section, Characteristics, Kind, "", 0, BeginSymName);
|
2013-11-19 20:52:52 +01:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) {
|
2015-03-27 22:34:24 +01:00
|
|
|
COFFSectionKey T{Section, "", 0};
|
2014-06-27 19:19:44 +02:00
|
|
|
auto Iter = COFFUniquingMap.find(T);
|
2014-04-11 01:55:11 +02:00
|
|
|
if (Iter == COFFUniquingMap.end())
|
2014-04-13 06:57:38 +02:00
|
|
|
return nullptr;
|
2013-11-19 20:52:52 +01:00
|
|
|
return Iter->second;
|
2013-07-06 14:13:10 +02:00
|
|
|
}
|
|
|
|
|
2015-05-21 21:20:38 +02:00
|
|
|
MCSectionCOFF *MCContext::getAssociativeCOFFSection(MCSectionCOFF *Sec,
|
|
|
|
const MCSymbol *KeySym) {
|
2014-09-04 19:42:03 +02: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-28 22:55:35 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dwarf Management
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
/// getDwarfFile - takes a file name an number to place in the dwarf file and
|
2010-07-28 22:55:35 +02: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-18 20:43:14 +02:00
|
|
|
unsigned MCContext::getDwarfFile(StringRef Directory, StringRef FileName,
|
2013-03-07 02:42:00 +01:00
|
|
|
unsigned FileNumber, unsigned CUID) {
|
2014-03-13 22:59:51 +01:00
|
|
|
MCDwarfLineTable &Table = MCDwarfLineTablesCUMap[CUID];
|
2014-03-13 20:15:04 +01:00
|
|
|
return Table.getFile(Directory, FileName, FileNumber);
|
2010-07-28 22:55:35 +02:00
|
|
|
}
|
2010-08-24 22:32:42 +02:00
|
|
|
|
2010-10-04 22:17:24 +02:00
|
|
|
/// isValidDwarfFileNumber - takes a dwarf file number and returns true if it
|
2010-08-24 22:32:42 +02:00
|
|
|
/// currently is assigned and false otherwise.
|
2013-03-07 02:42:00 +01:00
|
|
|
bool MCContext::isValidDwarfFileNumber(unsigned FileNumber, unsigned CUID) {
|
2015-05-18 20:43:23 +02:00
|
|
|
const SmallVectorImpl<MCDwarfFile> &MCDwarfFiles = getMCDwarfFiles(CUID);
|
|
|
|
if (FileNumber == 0 || FileNumber >= MCDwarfFiles.size())
|
2010-08-24 22:32:42 +02:00
|
|
|
return false;
|
|
|
|
|
2014-03-13 19:55:04 +01:00
|
|
|
return !MCDwarfFiles[FileNumber].Name.empty();
|
2010-08-24 22:32:42 +02:00
|
|
|
}
|
2012-01-27 00:20:11 +01:00
|
|
|
|
2015-05-21 18:52:32 +02:00
|
|
|
/// Remove empty sections from SectionStartEndSyms, to avoid generating
|
2014-06-19 17:52:37 +02:00
|
|
|
/// useless debug info for them.
|
|
|
|
void MCContext::finalizeDwarfSections(MCStreamer &MCOS) {
|
2015-05-31 20:49:28 +02:00
|
|
|
SectionsForRanges.remove_if(
|
|
|
|
[&](MCSection *Sec) { return !MCOS.mayHaveInstructions(*Sec); });
|
2014-06-19 17:52:37 +02:00
|
|
|
}
|
|
|
|
|
2015-05-18 20:43:14 +02:00
|
|
|
void MCContext::reportFatalError(SMLoc Loc, const Twine &Msg) const {
|
2012-01-27 00:20:11 +01: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-14 23:41:58 +01:00
|
|
|
report_fatal_error(Msg, false);
|
2012-01-27 00:20:11 +01: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);
|
|
|
|
}
|