LLVM  3.7.0
MCWinEH.cpp
Go to the documentation of this file.
1 //===- 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"
13 #include "llvm/MC/MCSectionCOFF.h"
14 #include "llvm/MC/MCStreamer.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/MC/MCWinEH.h"
17 #include "llvm/Support/COFF.h"
18 
19 namespace llvm {
20 namespace WinEH {
21 
22 /// We can't have one section for all .pdata or .xdata because the Microsoft
23 /// linker seems to want all code relocations to refer to the same object file
24 /// section. If the code described is comdat, create a new comdat section
25 /// associated with that comdat. If the code described is not in the main .text
26 /// section, make a new section for it. Otherwise use the main unwind info
27 /// section.
29  MCSectionCOFF *UnwindSec,
30  const MCSymbol *Function,
31  MCContext &Context) {
32  if (Function && Function->isInSection()) {
33  // If Function is in a COMDAT, get or create an unwind info section in that
34  // COMDAT group.
35  const MCSectionCOFF *FunctionSection =
36  cast<MCSectionCOFF>(&Function->getSection());
37  if (FunctionSection->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
38  return Context.getAssociativeCOFFSection(
39  UnwindSec, FunctionSection->getCOMDATSymbol());
40  }
41 
42  // If Function is in a section other than .text, create a new .pdata section.
43  // Otherwise use the plain .pdata section.
44  if (const auto *Section = dyn_cast<MCSectionCOFF>(FunctionSection)) {
45  StringRef CodeSecName = Section->getSectionName();
46  if (CodeSecName == ".text")
47  return UnwindSec;
48 
49  if (CodeSecName.startswith(".text$"))
50  CodeSecName = CodeSecName.substr(6);
51 
52  return Context.getCOFFSection(
53  (SecName + Twine('$') + CodeSecName).str(),
56  }
57  }
58 
59  return UnwindSec;
60 
61 }
62 
64  MCContext &Context) {
65  MCSectionCOFF *PData =
66  cast<MCSectionCOFF>(Context.getObjectFileInfo()->getPDataSection());
67  return getUnwindInfoSection(".pdata", PData, Function, Context);
68 }
69 
71  MCContext &Context) {
72  MCSectionCOFF *XData =
73  cast<MCSectionCOFF>(Context.getObjectFileInfo()->getXDataSection());
74  return getUnwindInfoSection(".xdata", XData, Function, Context);
75 }
76 
77 }
78 }
79 
Instances of this class represent a uniqued identifier for a section in the current translation unit...
Definition: MCSection.h:48
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:39
static SectionKind getDataRel()
Definition: SectionKind.h:227
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
MCSectionCOFF - This represents a section on Windows.
Definition: MCSectionCOFF.h:25
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
Context object for machine code objects.
Definition: MCContext.h:48
bool isInSection() const
isInSection - Check if this symbol is defined in some section (i.e., it is defined but not absolute)...
Definition: MCSymbol.h:255
MCSection * getPDataSection() const
MCSection * getXDataSection() const
MCSymbol * getCOMDATSymbol() const
Definition: MCSectionCOFF.h:65
unsigned getCharacteristics() const
Definition: MCSectionCOFF.h:64
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
MCSection & getSection() const
Get the section associated with a defined, non-absolute symbol.
Definition: MCSymbol.h:264
MCSectionCOFF * getAssociativeCOFFSection(MCSectionCOFF *Sec, const MCSymbol *KeySym)
Gets or creates a section equivalent to Sec that is associated with the section containing KeySym...
Definition: MCContext.cpp:428
static MCSection * getPDataSection(const MCSymbol *Function, MCContext &Context)
Definition: MCWinEH.cpp:63
static MCSection * getXDataSection(const MCSymbol *Function, MCContext &Context)
Definition: MCWinEH.cpp:70
MCSectionCOFF * getCOFFSection(StringRef Section, unsigned Characteristics, SectionKind Kind, StringRef COMDATSymName, int Selection, const char *BeginSymName=nullptr)
Definition: MCContext.cpp:383
static MCSection * getUnwindInfoSection(StringRef SecName, MCSectionCOFF *UnwindSec, const MCSymbol *Function, MCContext &Context)
We can't have one section for all .pdata or .xdata because the Microsoft linker seems to want all cod...
Definition: MCWinEH.cpp:28
const MCObjectFileInfo * getObjectFileInfo() const
Definition: MCContext.h:229
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40