2015-08-14 22:12:54 +08:00
|
|
|
//===- Driver.h -------------------------------------------------*- C++ -*-===//
|
2015-05-29 03:09:30 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_COFF_DRIVER_H
|
|
|
|
#define LLD_COFF_DRIVER_H
|
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
#include "Config.h"
|
2015-06-24 07:56:39 +08:00
|
|
|
#include "SymbolTable.h"
|
2015-06-13 20:50:13 +08:00
|
|
|
#include "lld/Core/LLVM.h"
|
2016-07-26 10:00:42 +08:00
|
|
|
#include "lld/Core/Reproduce.h"
|
2015-06-01 03:17:14 +08:00
|
|
|
#include "llvm/ADT/Optional.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include "llvm/ADT/StringRef.h"
|
2015-05-29 04:30:06 +08:00
|
|
|
#include "llvm/Object/COFF.h"
|
|
|
|
#include "llvm/Option/Arg.h"
|
|
|
|
#include "llvm/Option/ArgList.h"
|
2015-06-13 20:50:13 +08:00
|
|
|
#include "llvm/Support/StringSaver.h"
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <memory>
|
2015-06-01 03:17:09 +08:00
|
|
|
#include <set>
|
2015-05-29 03:09:30 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace coff {
|
|
|
|
|
2015-06-01 03:17:09 +08:00
|
|
|
class LinkerDriver;
|
|
|
|
extern LinkerDriver *Driver;
|
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
using llvm::COFF::MachineTypes;
|
2015-05-30 00:34:31 +08:00
|
|
|
using llvm::COFF::WindowsSubsystem;
|
2015-06-01 03:17:14 +08:00
|
|
|
using llvm::Optional;
|
2015-05-29 03:09:30 +08:00
|
|
|
class InputFile;
|
|
|
|
|
2015-09-20 05:36:28 +08:00
|
|
|
// Implemented in MarkLive.cpp.
|
|
|
|
void markLive(const std::vector<Chunk *> &Chunks);
|
|
|
|
|
|
|
|
// Implemented in ICF.cpp.
|
|
|
|
void doICF(const std::vector<Chunk *> &Chunks);
|
|
|
|
|
2015-06-07 10:55:19 +08:00
|
|
|
class ArgParser {
|
|
|
|
public:
|
2015-06-13 20:50:13 +08:00
|
|
|
ArgParser() : Alloc(AllocAux) {}
|
2015-06-07 10:55:19 +08:00
|
|
|
// Parses command line options.
|
2015-08-06 22:58:50 +08:00
|
|
|
llvm::opt::InputArgList parse(llvm::ArrayRef<const char *> Args);
|
2015-06-07 10:55:19 +08:00
|
|
|
|
2015-06-28 10:35:31 +08:00
|
|
|
// Concatenate LINK environment varirable and given arguments and parse them.
|
2015-08-06 22:58:50 +08:00
|
|
|
llvm::opt::InputArgList parseLINK(llvm::ArrayRef<const char *> Args);
|
2015-06-28 10:35:31 +08:00
|
|
|
|
2015-06-07 10:55:19 +08:00
|
|
|
// Tokenizes a given string and then parses as command line options.
|
2015-08-06 22:58:50 +08:00
|
|
|
llvm::opt::InputArgList parse(StringRef S) { return parse(tokenize(S)); }
|
2015-06-07 10:55:19 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::vector<const char *> tokenize(StringRef S);
|
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
std::vector<const char *> replaceResponseFiles(std::vector<const char *>);
|
2015-06-07 10:55:19 +08:00
|
|
|
|
2015-06-13 20:50:13 +08:00
|
|
|
llvm::BumpPtrAllocator AllocAux;
|
2015-08-13 09:07:08 +08:00
|
|
|
llvm::StringSaver Alloc;
|
2015-06-07 10:55:19 +08:00
|
|
|
};
|
|
|
|
|
2015-06-01 03:17:09 +08:00
|
|
|
class LinkerDriver {
|
|
|
|
public:
|
2015-06-20 06:39:48 +08:00
|
|
|
LinkerDriver() : Alloc(AllocAux) {}
|
2015-08-06 22:58:50 +08:00
|
|
|
void link(llvm::ArrayRef<const char *> Args);
|
2015-06-01 03:17:09 +08:00
|
|
|
|
|
|
|
// Used by the resolver to parse .drectve section contents.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseDirectives(StringRef S);
|
2015-06-01 03:17:09 +08:00
|
|
|
|
2016-07-26 10:00:42 +08:00
|
|
|
std::unique_ptr<CpioFile> Cpio; // for /linkrepro
|
|
|
|
|
2015-06-01 03:17:09 +08:00
|
|
|
private:
|
2015-06-13 20:50:13 +08:00
|
|
|
llvm::BumpPtrAllocator AllocAux;
|
2015-08-13 09:07:08 +08:00
|
|
|
llvm::StringSaver Alloc;
|
2015-06-07 10:55:19 +08:00
|
|
|
ArgParser Parser;
|
2015-06-24 07:56:39 +08:00
|
|
|
SymbolTable Symtab;
|
2015-06-01 03:17:09 +08:00
|
|
|
|
2015-06-01 05:04:56 +08:00
|
|
|
// Opens a file. Path has to be resolved already.
|
2015-08-06 22:58:50 +08:00
|
|
|
MemoryBufferRef openFile(StringRef Path);
|
2015-06-01 05:04:56 +08:00
|
|
|
|
2015-06-01 03:17:12 +08:00
|
|
|
// Searches a file from search paths.
|
2015-06-01 03:17:14 +08:00
|
|
|
Optional<StringRef> findFile(StringRef Filename);
|
|
|
|
Optional<StringRef> findLib(StringRef Filename);
|
|
|
|
StringRef doFindFile(StringRef Filename);
|
|
|
|
StringRef doFindLib(StringRef Filename);
|
2015-06-01 03:17:12 +08:00
|
|
|
|
|
|
|
// Parses LIB environment which contains a list of search paths.
|
2015-06-20 06:39:48 +08:00
|
|
|
void addLibSearchPaths();
|
2015-06-01 03:17:12 +08:00
|
|
|
|
2015-06-20 06:39:48 +08:00
|
|
|
// Library search path. The first element is always "" (current directory).
|
2015-06-01 03:17:12 +08:00
|
|
|
std::vector<StringRef> SearchPaths;
|
2015-06-01 03:17:09 +08:00
|
|
|
std::set<std::string> VisitedFiles;
|
2015-06-01 05:04:56 +08:00
|
|
|
|
2015-07-02 08:04:14 +08:00
|
|
|
Undefined *addUndefined(StringRef Sym);
|
2015-07-09 09:25:49 +08:00
|
|
|
StringRef mangle(StringRef Sym);
|
2015-06-26 11:44:00 +08:00
|
|
|
|
2015-06-29 09:03:53 +08:00
|
|
|
// Windows specific -- "main" is not the only main function in Windows.
|
|
|
|
// You can choose one from these four -- {w,}{WinMain,main}.
|
|
|
|
// There are four different entry point functions for them,
|
|
|
|
// {w,}{WinMain,main}CRTStartup, respectively. The linker needs to
|
|
|
|
// choose the right one depending on which "main" function is defined.
|
|
|
|
// This function looks up the symbol table and resolve corresponding
|
|
|
|
// entry point name.
|
|
|
|
StringRef findDefaultEntry();
|
|
|
|
WindowsSubsystem inferSubsystem();
|
|
|
|
|
2015-06-01 05:04:56 +08:00
|
|
|
// Driver is the owner of all opened files.
|
|
|
|
// InputFiles have MemoryBufferRefs to them.
|
|
|
|
std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
|
2015-06-01 03:17:09 +08:00
|
|
|
};
|
|
|
|
|
2015-08-13 09:07:08 +08:00
|
|
|
void parseModuleDefs(MemoryBufferRef MB, llvm::StringSaver *Alloc);
|
2015-08-06 22:58:50 +08:00
|
|
|
void writeImportLibrary();
|
2015-06-18 03:19:25 +08:00
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
// Functions below this line are defined in DriverUtils.cpp.
|
|
|
|
|
2015-05-30 00:11:52 +08:00
|
|
|
void printHelp(const char *Argv0);
|
|
|
|
|
2015-05-30 00:06:00 +08:00
|
|
|
// For /machine option.
|
2015-08-06 22:58:50 +08:00
|
|
|
MachineTypes getMachineType(StringRef Arg);
|
2015-07-26 05:54:50 +08:00
|
|
|
StringRef machineToStr(MachineTypes MT);
|
2015-05-30 00:06:00 +08:00
|
|
|
|
2015-05-30 00:18:15 +08:00
|
|
|
// Parses a string in the form of "<integer>[,<integer>]".
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size = nullptr);
|
2015-05-30 00:18:15 +08:00
|
|
|
|
2015-05-30 00:28:29 +08:00
|
|
|
// Parses a string in the form of "<integer>[.<integer>]".
|
|
|
|
// Minor's default value is 0.
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor);
|
2015-05-30 00:28:29 +08:00
|
|
|
|
2015-05-30 00:34:31 +08:00
|
|
|
// Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
|
|
|
|
uint32_t *Minor);
|
2015-05-30 00:34:31 +08:00
|
|
|
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseAlternateName(StringRef);
|
|
|
|
void parseMerge(StringRef);
|
2016-06-20 11:39:39 +08:00
|
|
|
void parseSection(StringRef);
|
2015-06-19 03:09:30 +08:00
|
|
|
|
2015-06-18 08:12:42 +08:00
|
|
|
// Parses a string in the form of "EMBED[,=<integer>]|NO".
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseManifest(StringRef Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
// Parses a string in the form of "level=<string>|uiAccess=<string>"
|
2015-08-06 22:58:50 +08:00
|
|
|
void parseManifestUAC(StringRef Arg);
|
2015-06-18 08:12:42 +08:00
|
|
|
|
|
|
|
// Create a resource file containing a manifest XML.
|
2015-08-06 22:58:50 +08:00
|
|
|
std::unique_ptr<MemoryBuffer> createManifestRes();
|
|
|
|
void createSideBySideManifest();
|
2015-06-18 08:12:42 +08:00
|
|
|
|
2015-06-17 08:16:33 +08:00
|
|
|
// Used for dllexported symbols.
|
2015-08-06 22:58:50 +08:00
|
|
|
Export parseExport(StringRef Arg);
|
|
|
|
void fixupExports();
|
2015-07-16 06:21:08 +08:00
|
|
|
void assignExportOrdinals();
|
2015-06-17 08:16:33 +08:00
|
|
|
|
2015-06-05 03:21:24 +08:00
|
|
|
// Parses a string in the form of "key=value" and check
|
|
|
|
// if value matches previous values for the key.
|
|
|
|
// This feature used in the directive section to reject
|
|
|
|
// incompatible objects.
|
2015-08-06 22:58:50 +08:00
|
|
|
void checkFailIfMismatch(StringRef Arg);
|
2015-06-05 03:21:24 +08:00
|
|
|
|
2015-06-15 05:50:50 +08:00
|
|
|
// Convert Windows resource files (.res files) to a .obj file
|
|
|
|
// using cvtres.exe.
|
2015-08-06 22:58:50 +08:00
|
|
|
std::unique_ptr<MemoryBuffer>
|
2015-06-15 05:50:50 +08:00
|
|
|
convertResToCOFF(const std::vector<MemoryBufferRef> &MBs);
|
|
|
|
|
2015-12-05 07:11:05 +08:00
|
|
|
void createPDB(StringRef Path);
|
2015-06-29 22:27:12 +08:00
|
|
|
|
2015-05-29 04:30:06 +08:00
|
|
|
// Create enum with OPT_xxx values for each option in Options.td
|
|
|
|
enum {
|
|
|
|
OPT_INVALID = 0,
|
|
|
|
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11) OPT_##ID,
|
|
|
|
#include "Options.inc"
|
|
|
|
#undef OPTION
|
|
|
|
};
|
|
|
|
|
2015-05-29 03:09:30 +08:00
|
|
|
} // namespace coff
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif
|