blob: 47eaf0f55648fd323176d1cd836367c7cb931cf2 [file] [log] [blame]
Saleem Abdulrasool64a8cc72014-08-07 02:59:41 +00001//===- lib/MC/MCWinEH.cpp - Windows EH implementation ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "llvm/ADT/StringRef.h"
11#include "llvm/MC/MCContext.h"
12#include "llvm/MC/MCObjectFileInfo.h"
13#include "llvm/MC/MCSectionCOFF.h"
14#include "llvm/MC/MCSymbol.h"
15#include "llvm/MC/MCWinEH.h"
16#include "llvm/Support/COFF.h"
17
18namespace llvm {
19namespace WinEH {
Saleem Abdulrasool64a8cc72014-08-07 02:59:41 +000020
Reid Kleckner5aabc062014-12-22 22:10:08 +000021/// We can't have one section for all .pdata or .xdata because the Microsoft
22/// linker seems to want all code relocations to refer to the same object file
23/// section. If the code described is comdat, create a new comdat section
24/// associated with that comdat. If the code described is not in the main .text
25/// section, make a new section for it. Otherwise use the main unwind info
26/// section.
Reid Kleckner7c4059e2014-09-04 17:42:03 +000027static const MCSection *getUnwindInfoSection(
28 StringRef SecName, const MCSectionCOFF *UnwindSec, const MCSymbol *Function,
29 MCContext &Context) {
Reid Kleckner7c4059e2014-09-04 17:42:03 +000030 if (Function && Function->isInSection()) {
Reid Kleckner5aabc062014-12-22 22:10:08 +000031 // If Function is in a COMDAT, get or create an unwind info section in that
32 // COMDAT group.
Reid Kleckner7c4059e2014-09-04 17:42:03 +000033 const MCSectionCOFF *FunctionSection =
34 cast<MCSectionCOFF>(&Function->getSection());
35 if (FunctionSection->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
36 return Context.getAssociativeCOFFSection(
37 UnwindSec, FunctionSection->getCOMDATSymbol());
38 }
Reid Kleckner5aabc062014-12-22 22:10:08 +000039
40 // If Function is in a section other than .text, create a new .pdata section.
41 // Otherwise use the plain .pdata section.
42 if (const auto *Section = dyn_cast<MCSectionCOFF>(FunctionSection)) {
43 StringRef CodeSecName = Section->getSectionName();
44 if (CodeSecName == ".text")
45 return UnwindSec;
46
47 if (CodeSecName.startswith(".text$"))
48 CodeSecName = CodeSecName.substr(6);
49
50 return Context.getCOFFSection(
51 (SecName + Twine('$') + CodeSecName).str(),
52 COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
53 SectionKind::getDataRel());
54 }
Reid Kleckner7c4059e2014-09-04 17:42:03 +000055 }
56
Reid Kleckner5aabc062014-12-22 22:10:08 +000057 return UnwindSec;
58
Reid Kleckner7c4059e2014-09-04 17:42:03 +000059}
60
61const MCSection *UnwindEmitter::getPDataSection(const MCSymbol *Function,
62 MCContext &Context) {
63 const MCSectionCOFF *PData =
64 cast<MCSectionCOFF>(Context.getObjectFileInfo()->getPDataSection());
65 return getUnwindInfoSection(".pdata", PData, Function, Context);
66}
67
68const MCSection *UnwindEmitter::getXDataSection(const MCSymbol *Function,
69 MCContext &Context) {
70 const MCSectionCOFF *XData =
71 cast<MCSectionCOFF>(Context.getObjectFileInfo()->getXDataSection());
72 return getUnwindInfoSection(".xdata", XData, Function, Context);
73}
74
Saleem Abdulrasool64a8cc72014-08-07 02:59:41 +000075}
76}
77