#include "llvm/ADT/StringRef.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCSectionCOFF.h"
#include "llvm/MC/MCSymbol.h"
#include "llvm/MC/MCWinEH.h"
#include "llvm/Support/COFF.h"
namespace llvm {
namespace WinEH {
static const MCSection *getUnwindInfoSection(
StringRef SecName, const MCSectionCOFF *UnwindSec, const MCSymbol *Function,
MCContext &Context) {
if (Function && Function->isInSection()) {
const MCSectionCOFF *FunctionSection =
cast<MCSectionCOFF>(&Function->getSection());
if (FunctionSection->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
return Context.getAssociativeCOFFSection(
UnwindSec, FunctionSection->getCOMDATSymbol());
}
if (const auto *Section = dyn_cast<MCSectionCOFF>(FunctionSection)) {
StringRef CodeSecName = Section->getSectionName();
if (CodeSecName == ".text")
return UnwindSec;
if (CodeSecName.startswith(".text$"))
CodeSecName = CodeSecName.substr(6);
return Context.getCOFFSection(
(SecName + Twine('$') + CodeSecName).str(),
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
SectionKind::getDataRel());
}
}
return UnwindSec;
}
const MCSection *UnwindEmitter::getPDataSection(const MCSymbol *Function,
MCContext &Context) {
const MCSectionCOFF *PData =
cast<MCSectionCOFF>(Context.getObjectFileInfo()->getPDataSection());
return getUnwindInfoSection(".pdata", PData, Function, Context);
}
const MCSection *UnwindEmitter::getXDataSection(const MCSymbol *Function,
MCContext &Context) {
const MCSectionCOFF *XData =
cast<MCSectionCOFF>(Context.getObjectFileInfo()->getXDataSection());
return getUnwindInfoSection(".xdata", XData, Function, Context);
}
}
}