Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Driver.h « COFF « lld - github.com/llvm/llvm-project.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3cda42017d22bf7a330d5726cf1ca2aa7e937e18 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===- Driver.h -----------------------------------------------------------===//
//
//                             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

#include "Config.h"
#include "SymbolTable.h"
#include "lld/Core/LLVM.h"
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Object/COFF.h"
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/StringSaver.h"
#include <memory>
#include <set>
#include <system_error>
#include <vector>

namespace lld {
namespace coff {

class LinkerDriver;
extern LinkerDriver *Driver;

using llvm::COFF::MachineTypes;
using llvm::COFF::WindowsSubsystem;
using llvm::Optional;
class InputFile;

// Entry point of the COFF linker.
bool link(llvm::ArrayRef<const char *> Args);

class ArgParser {
public:
  ArgParser() : Alloc(AllocAux) {}
  // Parses command line options.
  ErrorOr<llvm::opt::InputArgList> parse(llvm::ArrayRef<const char *> Args);

  // Concatenate LINK environment varirable and given arguments and parse them.
  ErrorOr<llvm::opt::InputArgList> parseLINK(llvm::ArrayRef<const char *> Args);

  // Tokenizes a given string and then parses as command line options.
  ErrorOr<llvm::opt::InputArgList> parse(StringRef S) {
    return parse(tokenize(S));
  }

private:
  std::vector<const char *> tokenize(StringRef S);

  ErrorOr<std::vector<const char *>>
  replaceResponseFiles(std::vector<const char *>);

  llvm::BumpPtrAllocator AllocAux;
  llvm::BumpPtrStringSaver Alloc;
};

class LinkerDriver {
public:
  LinkerDriver() : Alloc(AllocAux) {}
  bool link(llvm::ArrayRef<const char *> Args);

  // Used by the resolver to parse .drectve section contents.
  std::error_code parseDirectives(StringRef S);

private:
  llvm::BumpPtrAllocator AllocAux;
  llvm::BumpPtrStringSaver Alloc;
  ArgParser Parser;
  SymbolTable Symtab;

  // Opens a file. Path has to be resolved already.
  ErrorOr<MemoryBufferRef> openFile(StringRef Path);

  // Searches a file from search paths.
  Optional<StringRef> findFile(StringRef Filename);
  Optional<StringRef> findLib(StringRef Filename);
  StringRef doFindFile(StringRef Filename);
  StringRef doFindLib(StringRef Filename);

  // Parses LIB environment which contains a list of search paths.
  void addLibSearchPaths();

  // Library search path. The first element is always "" (current directory).
  std::vector<StringRef> SearchPaths;
  std::set<std::string> VisitedFiles;

  Undefined *addUndefined(StringRef Sym);
  StringRef mangle(StringRef Sym);

  // 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();

  // Driver is the owner of all opened files.
  // InputFiles have MemoryBufferRefs to them.
  std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
};

std::error_code parseModuleDefs(MemoryBufferRef MB);
std::error_code writeImportLibrary();

// Functions below this line are defined in DriverUtils.cpp.

void printHelp(const char *Argv0);

// For /machine option.
ErrorOr<MachineTypes> getMachineType(StringRef Arg);
StringRef machineTypeToStr(MachineTypes MT);

// Parses a string in the form of "<integer>[,<integer>]".
std::error_code parseNumbers(StringRef Arg, uint64_t *Addr,
                             uint64_t *Size = nullptr);

// Parses a string in the form of "<integer>[.<integer>]".
// Minor's default value is 0.
std::error_code parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor);

// Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
std::error_code parseSubsystem(StringRef Arg, WindowsSubsystem *Sys,
                               uint32_t *Major, uint32_t *Minor);

std::error_code parseAlternateName(StringRef);
std::error_code parseMerge(StringRef);

// Parses a string in the form of "EMBED[,=<integer>]|NO".
std::error_code parseManifest(StringRef Arg);

// Parses a string in the form of "level=<string>|uiAccess=<string>"
std::error_code parseManifestUAC(StringRef Arg);

// Create a resource file containing a manifest XML.
ErrorOr<std::unique_ptr<MemoryBuffer>> createManifestRes();
std::error_code createSideBySideManifest();

// Used for dllexported symbols.
ErrorOr<Export> parseExport(StringRef Arg);
std::error_code fixupExports();

// 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.
std::error_code checkFailIfMismatch(StringRef Arg);

// Convert Windows resource files (.res files) to a .obj file
// using cvtres.exe.
ErrorOr<std::unique_ptr<MemoryBuffer>>
convertResToCOFF(const std::vector<MemoryBufferRef> &MBs);

void touchFile(StringRef Path);

// 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
};

} // namespace coff
} // namespace lld

#endif