COFFImportFile.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- COFFImportFile.h - COFF short import file implementation -*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // COFF short import file is a special kind of file which contains
  15. // only symbol names for DLL-exported symbols. This class implements
  16. // exporting of Symbols to create libraries and a SymbolicFile
  17. // interface for the file type.
  18. //
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_OBJECT_COFFIMPORTFILE_H
  21. #define LLVM_OBJECT_COFFIMPORTFILE_H
  22. #include "llvm/ADT/ArrayRef.h"
  23. #include "llvm/Object/COFF.h"
  24. #include "llvm/Object/IRObjectFile.h"
  25. #include "llvm/Object/ObjectFile.h"
  26. #include "llvm/Object/SymbolicFile.h"
  27. #include "llvm/Support/MemoryBuffer.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. namespace llvm {
  30. namespace object {
  31. class COFFImportFile : public SymbolicFile {
  32. public:
  33. COFFImportFile(MemoryBufferRef Source)
  34. : SymbolicFile(ID_COFFImportFile, Source) {}
  35. static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
  36. void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
  37. Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override {
  38. if (Symb.p == 0)
  39. OS << "__imp_";
  40. OS << StringRef(Data.getBufferStart() + sizeof(coff_import_header));
  41. return Error::success();
  42. }
  43. Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override {
  44. return SymbolRef::SF_Global;
  45. }
  46. basic_symbol_iterator symbol_begin() const override {
  47. return BasicSymbolRef(DataRefImpl(), this);
  48. }
  49. basic_symbol_iterator symbol_end() const override {
  50. DataRefImpl Symb;
  51. Symb.p = isData() ? 1 : 2;
  52. return BasicSymbolRef(Symb, this);
  53. }
  54. const coff_import_header *getCOFFImportHeader() const {
  55. return reinterpret_cast<const object::coff_import_header *>(
  56. Data.getBufferStart());
  57. }
  58. private:
  59. bool isData() const {
  60. return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA;
  61. }
  62. };
  63. struct COFFShortExport {
  64. /// The name of the export as specified in the .def file or on the command
  65. /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This
  66. /// may lack mangling, such as underscore prefixing and stdcall suffixing.
  67. std::string Name;
  68. /// The external, exported name. Only non-empty when export renaming is in
  69. /// effect, i.e. "foo" in "/EXPORT:foo=bar".
  70. std::string ExtName;
  71. /// The real, mangled symbol name from the object file. Given
  72. /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall.
  73. std::string SymbolName;
  74. /// Creates a weak alias. This is the name of the weak aliasee. In a .def
  75. /// file, this is "baz" in "EXPORTS\nfoo = bar == baz".
  76. std::string AliasTarget;
  77. uint16_t Ordinal = 0;
  78. bool Noname = false;
  79. bool Data = false;
  80. bool Private = false;
  81. bool Constant = false;
  82. friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
  83. return L.Name == R.Name && L.ExtName == R.ExtName &&
  84. L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
  85. L.Data == R.Data && L.Private == R.Private;
  86. }
  87. friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
  88. return !(L == R);
  89. }
  90. };
  91. Error writeImportLibrary(StringRef ImportName, StringRef Path,
  92. ArrayRef<COFFShortExport> Exports,
  93. COFF::MachineTypes Machine, bool MinGW);
  94. } // namespace object
  95. } // namespace llvm
  96. #endif
  97. #ifdef __GNUC__
  98. #pragma GCC diagnostic pop
  99. #endif