MCWinEH.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/MC/MCContext.h"
  11. #include "llvm/MC/MCObjectFileInfo.h"
  12. #include "llvm/MC/MCSectionCOFF.h"
  13. #include "llvm/MC/MCStreamer.h"
  14. #include "llvm/MC/MCSymbol.h"
  15. #include "llvm/MC/MCWinEH.h"
  16. #include "llvm/Support/COFF.h"
  17. namespace llvm {
  18. namespace WinEH {
  19. /// We can't have one section for all .pdata or .xdata because the Microsoft
  20. /// linker seems to want all code relocations to refer to the same object file
  21. /// section. If the code described is comdat, create a new comdat section
  22. /// associated with that comdat. If the code described is not in the main .text
  23. /// section, make a new section for it. Otherwise use the main unwind info
  24. /// section.
  25. static MCSection *getUnwindInfoSection(StringRef SecName,
  26. MCSectionCOFF *UnwindSec,
  27. const MCSymbol *Function,
  28. MCContext &Context) {
  29. if (Function && Function->isInSection()) {
  30. // If Function is in a COMDAT, get or create an unwind info section in that
  31. // COMDAT group.
  32. const MCSectionCOFF *FunctionSection =
  33. cast<MCSectionCOFF>(&Function->getSection());
  34. if (FunctionSection->getCharacteristics() & COFF::IMAGE_SCN_LNK_COMDAT) {
  35. return Context.getAssociativeCOFFSection(
  36. UnwindSec, FunctionSection->getCOMDATSymbol());
  37. }
  38. // If Function is in a section other than .text, create a new .pdata section.
  39. // Otherwise use the plain .pdata section.
  40. if (const auto *Section = dyn_cast<MCSectionCOFF>(FunctionSection)) {
  41. StringRef CodeSecName = Section->getSectionName();
  42. if (CodeSecName == ".text")
  43. return UnwindSec;
  44. if (CodeSecName.startswith(".text$"))
  45. CodeSecName = CodeSecName.substr(6);
  46. return Context.getCOFFSection(
  47. (SecName + Twine('$') + CodeSecName).str(),
  48. COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_READ,
  49. SectionKind::getDataRel());
  50. }
  51. }
  52. return UnwindSec;
  53. }
  54. MCSection *UnwindEmitter::getPDataSection(const MCSymbol *Function,
  55. MCContext &Context) {
  56. MCSectionCOFF *PData =
  57. cast<MCSectionCOFF>(Context.getObjectFileInfo()->getPDataSection());
  58. return getUnwindInfoSection(".pdata", PData, Function, Context);
  59. }
  60. MCSection *UnwindEmitter::getXDataSection(const MCSymbol *Function,
  61. MCContext &Context) {
  62. MCSectionCOFF *XData =
  63. cast<MCSectionCOFF>(Context.getObjectFileInfo()->getXDataSection());
  64. return getUnwindInfoSection(".xdata", XData, Function, Context);
  65. }
  66. }
  67. }