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