MCELFObjectWriter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //===-- llvm/MC/MCELFObjectWriter.h - ELF Object Writer ---------*- C++ -*-===//
  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. #ifndef LLVM_MC_MCELFOBJECTWRITER_H
  10. #define LLVM_MC_MCELFOBJECTWRITER_H
  11. #include "llvm/ADT/Triple.h"
  12. #include "llvm/Support/DataTypes.h"
  13. #include "llvm/Support/ELF.h"
  14. #include <vector>
  15. namespace llvm {
  16. class MCAssembler;
  17. class MCFixup;
  18. class MCFragment;
  19. class MCObjectWriter;
  20. class MCSymbol;
  21. class MCSymbolELF;
  22. class MCValue;
  23. class raw_pwrite_stream;
  24. struct ELFRelocationEntry {
  25. uint64_t Offset; // Where is the relocation.
  26. const MCSymbolELF *Symbol; // The symbol to relocate with.
  27. unsigned Type; // The type of the relocation.
  28. uint64_t Addend; // The addend to use.
  29. ELFRelocationEntry(uint64_t Offset, const MCSymbolELF *Symbol, unsigned Type,
  30. uint64_t Addend)
  31. : Offset(Offset), Symbol(Symbol), Type(Type), Addend(Addend) {}
  32. };
  33. class MCELFObjectTargetWriter {
  34. const uint8_t OSABI;
  35. const uint16_t EMachine;
  36. const unsigned HasRelocationAddend : 1;
  37. const unsigned Is64Bit : 1;
  38. const unsigned IsN64 : 1;
  39. protected:
  40. MCELFObjectTargetWriter(bool Is64Bit_, uint8_t OSABI_,
  41. uint16_t EMachine_, bool HasRelocationAddend,
  42. bool IsN64=false);
  43. public:
  44. static uint8_t getOSABI(Triple::OSType OSType) {
  45. switch (OSType) {
  46. case Triple::CloudABI:
  47. return ELF::ELFOSABI_CLOUDABI;
  48. case Triple::PS4:
  49. case Triple::FreeBSD:
  50. return ELF::ELFOSABI_FREEBSD;
  51. case Triple::Linux:
  52. return ELF::ELFOSABI_LINUX;
  53. default:
  54. return ELF::ELFOSABI_NONE;
  55. }
  56. }
  57. virtual ~MCELFObjectTargetWriter() {}
  58. virtual unsigned GetRelocType(const MCValue &Target, const MCFixup &Fixup,
  59. bool IsPCRel) const = 0;
  60. virtual bool needsRelocateWithSymbol(const MCSymbol &Sym,
  61. unsigned Type) const;
  62. virtual void sortRelocs(const MCAssembler &Asm,
  63. std::vector<ELFRelocationEntry> &Relocs);
  64. /// \name Accessors
  65. /// @{
  66. uint8_t getOSABI() const { return OSABI; }
  67. uint16_t getEMachine() const { return EMachine; }
  68. bool hasRelocationAddend() const { return HasRelocationAddend; }
  69. bool is64Bit() const { return Is64Bit; }
  70. bool isN64() const { return IsN64; }
  71. /// @}
  72. // Instead of changing everyone's API we pack the N64 Type fields
  73. // into the existing 32 bit data unsigned.
  74. #define R_TYPE_SHIFT 0
  75. #define R_TYPE_MASK 0xffffff00
  76. #define R_TYPE2_SHIFT 8
  77. #define R_TYPE2_MASK 0xffff00ff
  78. #define R_TYPE3_SHIFT 16
  79. #define R_TYPE3_MASK 0xff00ffff
  80. #define R_SSYM_SHIFT 24
  81. #define R_SSYM_MASK 0x00ffffff
  82. // N64 relocation type accessors
  83. uint8_t getRType(uint32_t Type) const {
  84. return (unsigned)((Type >> R_TYPE_SHIFT) & 0xff);
  85. }
  86. uint8_t getRType2(uint32_t Type) const {
  87. return (unsigned)((Type >> R_TYPE2_SHIFT) & 0xff);
  88. }
  89. uint8_t getRType3(uint32_t Type) const {
  90. return (unsigned)((Type >> R_TYPE3_SHIFT) & 0xff);
  91. }
  92. uint8_t getRSsym(uint32_t Type) const {
  93. return (unsigned)((Type >> R_SSYM_SHIFT) & 0xff);
  94. }
  95. // N64 relocation type setting
  96. unsigned setRType(unsigned Value, unsigned Type) const {
  97. return ((Type & R_TYPE_MASK) | ((Value & 0xff) << R_TYPE_SHIFT));
  98. }
  99. unsigned setRType2(unsigned Value, unsigned Type) const {
  100. return (Type & R_TYPE2_MASK) | ((Value & 0xff) << R_TYPE2_SHIFT);
  101. }
  102. unsigned setRType3(unsigned Value, unsigned Type) const {
  103. return (Type & R_TYPE3_MASK) | ((Value & 0xff) << R_TYPE3_SHIFT);
  104. }
  105. unsigned setRSsym(unsigned Value, unsigned Type) const {
  106. return (Type & R_SSYM_MASK) | ((Value & 0xff) << R_SSYM_SHIFT);
  107. }
  108. };
  109. /// \brief Construct a new ELF writer instance.
  110. ///
  111. /// \param MOTW - The target specific ELF writer subclass.
  112. /// \param OS - The stream to write to.
  113. /// \returns The constructed object writer.
  114. MCObjectWriter *createELFObjectWriter(MCELFObjectTargetWriter *MOTW,
  115. raw_pwrite_stream &OS,
  116. bool IsLittleEndian);
  117. } // End llvm namespace
  118. #endif