COFFImportFile.h 3.6 KB

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