COFFImportFile.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/ObjectFile.h"
  25. #include "llvm/Object/SymbolicFile.h"
  26. #include "llvm/Support/MemoryBufferRef.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. namespace llvm {
  29. namespace object {
  30. class COFFImportFile : public SymbolicFile {
  31. public:
  32. COFFImportFile(MemoryBufferRef Source)
  33. : SymbolicFile(ID_COFFImportFile, Source) {}
  34. static bool classof(Binary const *V) { return V->isCOFFImportFile(); }
  35. void moveSymbolNext(DataRefImpl &Symb) const override { ++Symb.p; }
  36. Error printSymbolName(raw_ostream &OS, DataRefImpl Symb) const override {
  37. if (Symb.p == 0)
  38. OS << "__imp_";
  39. OS << StringRef(Data.getBufferStart() + sizeof(coff_import_header));
  40. return Error::success();
  41. }
  42. Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override {
  43. return SymbolRef::SF_Global;
  44. }
  45. basic_symbol_iterator symbol_begin() const override {
  46. return BasicSymbolRef(DataRefImpl(), this);
  47. }
  48. basic_symbol_iterator symbol_end() const override {
  49. DataRefImpl Symb;
  50. Symb.p = isData() ? 1 : 2;
  51. return BasicSymbolRef(Symb, this);
  52. }
  53. const coff_import_header *getCOFFImportHeader() const {
  54. return reinterpret_cast<const object::coff_import_header *>(
  55. Data.getBufferStart());
  56. }
  57. private:
  58. bool isData() const {
  59. return getCOFFImportHeader()->getType() == COFF::IMPORT_DATA;
  60. }
  61. };
  62. struct COFFShortExport {
  63. /// The name of the export as specified in the .def file or on the command
  64. /// line, i.e. "foo" in "/EXPORT:foo", and "bar" in "/EXPORT:foo=bar". This
  65. /// may lack mangling, such as underscore prefixing and stdcall suffixing.
  66. std::string Name;
  67. /// The external, exported name. Only non-empty when export renaming is in
  68. /// effect, i.e. "foo" in "/EXPORT:foo=bar".
  69. std::string ExtName;
  70. /// The real, mangled symbol name from the object file. Given
  71. /// "/export:foo=bar", this could be "_bar@8" if bar is stdcall.
  72. std::string SymbolName;
  73. /// Creates a weak alias. This is the name of the weak aliasee. In a .def
  74. /// file, this is "baz" in "EXPORTS\nfoo = bar == baz".
  75. std::string AliasTarget;
  76. uint16_t Ordinal = 0;
  77. bool Noname = false;
  78. bool Data = false;
  79. bool Private = false;
  80. bool Constant = false;
  81. friend bool operator==(const COFFShortExport &L, const COFFShortExport &R) {
  82. return L.Name == R.Name && L.ExtName == R.ExtName &&
  83. L.Ordinal == R.Ordinal && L.Noname == R.Noname &&
  84. L.Data == R.Data && L.Private == R.Private;
  85. }
  86. friend bool operator!=(const COFFShortExport &L, const COFFShortExport &R) {
  87. return !(L == R);
  88. }
  89. };
  90. Error writeImportLibrary(StringRef ImportName, StringRef Path,
  91. ArrayRef<COFFShortExport> Exports,
  92. COFF::MachineTypes Machine, bool MinGW);
  93. } // namespace object
  94. } // namespace llvm
  95. #endif
  96. #ifdef __GNUC__
  97. #pragma GCC diagnostic pop
  98. #endif