Skip to content

Files

Ignoring revisions in .git-blame-ignore-revs.

Latest commit

 

History

History
1000 lines (893 loc) · 33.5 KB

DriverUtils.cpp

File metadata and controls

1000 lines (893 loc) · 33.5 KB
 
1
2
//===- DriverUtils.cpp ----------------------------------------------------===//
//
Jan 19, 2019
Jan 19, 2019
3
4
5
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
7
8
9
10
11
12
13
14
//
//===----------------------------------------------------------------------===//
//
// This file contains utility functions for the driver. Because there
// are so many small functions, we created this separate file to make
// Driver.cpp less cluttered.
//
//===----------------------------------------------------------------------===//
Jan 10, 2023
Jan 10, 2023
15
#include "COFFLinkerContext.h"
16
#include "Driver.h"
Jul 15, 2015
Jul 15, 2015
17
#include "Symbols.h"
Oct 25, 2017
Oct 25, 2017
18
#include "lld/Common/ErrorHandler.h"
Nov 28, 2017
Nov 28, 2017
19
#include "lld/Common/Memory.h"
Jul 23, 2022
Jul 23, 2022
20
#include "llvm/ADT/STLExtras.h"
Jun 25, 2023
Jun 25, 2023
21
#include "llvm/ADT/StringExtras.h"
22
#include "llvm/ADT/StringSwitch.h"
Jul 8, 2017
Jul 8, 2017
23
#include "llvm/BinaryFormat/COFF.h"
24
#include "llvm/Object/COFF.h"
Jul 8, 2017
Jul 8, 2017
25
#include "llvm/Object/WindowsResource.h"
26
27
28
29
#include "llvm/Option/Arg.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Option/Option.h"
#include "llvm/Support/CommandLine.h"
Jun 17, 2015
Jun 17, 2015
30
#include "llvm/Support/FileUtilities.h"
Jul 8, 2017
Jul 8, 2017
31
#include "llvm/Support/MathExtras.h"
32
#include "llvm/Support/Process.h"
Jun 14, 2015
Jun 14, 2015
33
#include "llvm/Support/Program.h"
Oct 6, 2023
Oct 6, 2023
34
#include "llvm/Support/TimeProfiler.h"
35
#include "llvm/Support/raw_ostream.h"
Aug 22, 2017
Aug 22, 2017
36
#include "llvm/WindowsManifest/WindowsManifestMerger.h"
Aug 31, 2020
Aug 31, 2020
37
#include <limits>
38
#include <memory>
Dec 6, 2022
Dec 6, 2022
39
#include <optional>
40
41
using namespace llvm::COFF;
Aug 15, 2023
Aug 15, 2023
42
using namespace llvm::opt;
43
44
45
46
47
using namespace llvm;
using llvm::sys::Process;
namespace lld {
namespace coff {
Jun 17, 2015
Jun 17, 2015
48
49
namespace {
Jul 8, 2017
Jul 8, 2017
50
51
52
const uint16_t SUBLANG_ENGLISH_US = 0x0409;
const uint16_t RT_MANIFEST = 24;
Jun 17, 2015
Jun 17, 2015
53
54
class Executor {
public:
Jan 20, 2022
Jan 20, 2022
55
56
57
58
59
explicit Executor(StringRef s) : prog(saver().save(s)) {}
void add(StringRef s) { args.push_back(saver().save(s)); }
void add(std::string &s) { args.push_back(saver().save(s)); }
void add(Twine s) { args.push_back(saver().save(s)); }
void add(const char *s) { args.push_back(saver().save(s)); }
Jun 17, 2015
Jun 17, 2015
60
Aug 6, 2015
Aug 6, 2015
61
void run() {
Dec 8, 2016
Dec 8, 2016
62
ErrorOr<std::string> exeOrErr = sys::findProgramByName(prog);
Jul 15, 2016
Jul 15, 2016
63
if (auto ec = exeOrErr.getError())
Oct 25, 2017
Oct 25, 2017
64
fatal("unable to find " + prog + " in PATH: " + ec.message());
Jan 20, 2022
Jan 20, 2022
65
StringRef exe = saver().save(*exeOrErr);
Jun 17, 2015
Jun 17, 2015
66
args.insert(args.begin(), exe);
Feb 21, 2017
Feb 21, 2017
67
Jun 12, 2018
Jun 12, 2018
68
if (sys::ExecuteAndWait(args[0], args) != 0)
Feb 21, 2017
Feb 21, 2017
69
70
fatal("ExecuteAndWait failed: " +
llvm::join(args.begin(), args.end(), " "));
Jun 17, 2015
Jun 17, 2015
71
72
73
74
}
private:
StringRef prog;
Feb 21, 2017
Feb 21, 2017
75
std::vector<StringRef> args;
Jun 17, 2015
Jun 17, 2015
76
77
78
};
} // anonymous namespace
May 29, 2015
May 29, 2015
80
// Parses a string in the form of "<integer>[,<integer>]".
Jan 10, 2023
Jan 10, 2023
81
void LinkerDriver::parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size) {
Aug 8, 2022
Aug 8, 2022
82
auto [s1, s2] = arg.split(',');
Aug 6, 2015
Aug 6, 2015
83
if (s1.getAsInteger(0, *addr))
Jul 14, 2016
Jul 14, 2016
84
fatal("invalid number: " + s1);
Aug 6, 2015
Aug 6, 2015
85
if (size && !s2.empty() && s2.getAsInteger(0, *size))
Jul 14, 2016
Jul 14, 2016
86
fatal("invalid number: " + s2);
May 29, 2015
May 29, 2015
87
88
}
May 29, 2015
May 29, 2015
89
90
// Parses a string in the form of "<integer>[.<integer>]".
// If second number is not present, Minor is set to 0.
Jan 10, 2023
Jan 10, 2023
91
92
void LinkerDriver::parseVersion(StringRef arg, uint32_t *major,
uint32_t *minor) {
Aug 8, 2022
Aug 8, 2022
93
auto [s1, s2] = arg.split('.');
Oct 5, 2020
Oct 5, 2020
94
if (s1.getAsInteger(10, *major))
Jul 14, 2016
Jul 14, 2016
95
fatal("invalid number: " + s1);
May 29, 2015
May 29, 2015
96
*minor = 0;
Oct 5, 2020
Oct 5, 2020
97
if (!s2.empty() && s2.getAsInteger(10, *minor))
Jul 14, 2016
Jul 14, 2016
98
fatal("invalid number: " + s2);
May 29, 2015
May 29, 2015
99
}
Jul 11, 2019
Jul 11, 2019
100
Jan 10, 2023
Jan 10, 2023
101
void LinkerDriver::parseGuard(StringRef fullArg) {