summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/PECOFF/SetSubsystemPass.h
blob: bf5721c951c59f8f61905083a2c72fa6cfc61f54 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//===- lib/ReaderWriter/PECOFF/SetSubsystemPass.h -------------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_PE_COFF_SET_SUBSYSTEM_PASS_H
#define LLD_READER_WRITER_PE_COFF_SET_SUBSYSTEM_PASS_H

#include "lld/ReaderWriter/PECOFFLinkingContext.h"

using llvm::COFF::WindowsSubsystem;

namespace lld {
namespace pecoff {

/// If "main" or "wmain" is defined, /subsystem:console is the default. If
/// "WinMain" or "wWinMain" is defined, /subsystem:windows is the default.
class SetSubsystemPass : public lld::Pass {
public:
  SetSubsystemPass(PECOFFLinkingContext &ctx) : _ctx(ctx) {}

  void perform(std::unique_ptr<MutableFile> &file) override {
    if (_ctx.getSubsystem() != WindowsSubsystem::IMAGE_SUBSYSTEM_UNKNOWN)
      return;
    StringRef main = _ctx.decorateSymbol("main");
    StringRef wmain = _ctx.decorateSymbol("wmain");
    StringRef winmain = _ctx.decorateSymbol("WinMain");
    StringRef wwinmain = _ctx.decorateSymbol("wWinMain");
    for (auto *atom : file->defined()) {
      StringRef s = atom->name();
      if (s == main || s == wmain) {
        _ctx.setSubsystem(WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_CUI);
        return;
      }
      if (s == winmain || s == wwinmain) {
        _ctx.setSubsystem(WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_GUI);
        return;
      }
    }
    if (_ctx.isDll()) {
      _ctx.setSubsystem(WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_GUI);
      return;
    }
    llvm_unreachable("Failed to infer the subsystem.");
  }

private:
  PECOFFLinkingContext &_ctx;
};

} // namespace pecoff
} // namespace lld

#endif
OpenPOWER on IntegriCloud