If /subsystem option is not given, the linker needs to infer the subsystem based on the entry point symbol. If it fails to infer that, the linker should error out on it. LLD was almost correct, but it would fail to infer the subsystem if the entry point is specified with /entry. This is because the subsystem inference was coupled with the entry point function searching (if no entry point name is specified, the linker needs to find the right entry name). This patch makes the subsystem inference an independent pass to fix the issue. Now, as long as an entry point function is defined, LLD can infer the subsystem no matter how it resolved the entry point. I don't think scanning all the defined symbols is fast, although it shouldn't be that slow. The file class there does not provide any easy way to find an atom by name, so this is what we can do at this moment. I'd like to revisit this later to make it more efficient. llvm-svn: 221499
67 lines
2.3 KiB
C++
67 lines
2.3 KiB
C++
//===- lib/ReaderWriter/PECOFF/InferSubsystemPass.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_INFER_SUBSYSTEM_PASS_H
|
|
#define LLD_READER_WRITER_PE_COFF_INFER_SUBSYSTEM_PASS_H
|
|
|
|
#include "Atoms.h"
|
|
#include "lld/Core/Pass.h"
|
|
#include <vector>
|
|
|
|
namespace lld {
|
|
namespace pecoff {
|
|
|
|
// Infers subsystem from entry point function name.
|
|
class InferSubsystemPass : public lld::Pass {
|
|
public:
|
|
InferSubsystemPass(PECOFFLinkingContext &ctx) : _ctx(ctx) {}
|
|
|
|
void perform(std::unique_ptr<MutableFile> &file) override {
|
|
if (_ctx.getSubsystem() != WindowsSubsystem::IMAGE_SUBSYSTEM_UNKNOWN)
|
|
return;
|
|
|
|
if (_ctx.isDll()) {
|
|
_ctx.setSubsystem(WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_GUI);
|
|
return;
|
|
}
|
|
|
|
// Scan the resolved symbols to infer the subsystem.
|
|
const std::string wWinMain = _ctx.decorateSymbol("wWinMainCRTStartup");
|
|
const std::string wWinMainAt = _ctx.decorateSymbol("wWinMainCRTStartup@");
|
|
const std::string winMain = _ctx.decorateSymbol("WinMainCRTStartup");
|
|
const std::string winMainAt = _ctx.decorateSymbol("WinMainCRTStartup@");
|
|
const std::string wmain = _ctx.decorateSymbol("wmainCRTStartup");
|
|
const std::string wmainAt = _ctx.decorateSymbol("wmainCRTStartup@");
|
|
const std::string main = _ctx.decorateSymbol("mainCRTStartup");
|
|
const std::string mainAt = _ctx.decorateSymbol("mainCRTStartup@");
|
|
|
|
for (const DefinedAtom *atom : file->definedAtoms()) {
|
|
if (atom->name() == wWinMain || atom->name().startswith(wWinMainAt) ||
|
|
atom->name() == winMain || atom->name().startswith(winMainAt)) {
|
|
_ctx.setSubsystem(WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_GUI);
|
|
return;
|
|
}
|
|
if (atom->name() == wmain || atom->name().startswith(wmainAt) ||
|
|
atom->name() == main || atom->name().startswith(mainAt)) {
|
|
_ctx.setSubsystem(WindowsSubsystem::IMAGE_SUBSYSTEM_WINDOWS_CUI);
|
|
return;
|
|
}
|
|
}
|
|
llvm::report_fatal_error("Failed to infer subsystem");
|
|
}
|
|
|
|
private:
|
|
PECOFFLinkingContext &_ctx;
|
|
};
|
|
|
|
} // namespace pecoff
|
|
} // namespace lld
|
|
|
|
#endif
|