annotate lld/MachO/DriverUtils.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- DriverUtils.cpp ----------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "Config.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "Driver.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "InputFiles.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "ObjC.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 #include "lld/Common/Args.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "lld/Common/ErrorHandler.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "lld/Common/Memory.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "lld/Common/Reproduce.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/ADT/CachedHashString.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/ADT/DenseMap.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/Bitcode/BitcodeReader.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/LTO/LTO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/Option/Arg.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24 #include "llvm/Option/ArgList.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 #include "llvm/Option/Option.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 #include "llvm/Support/CommandLine.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 #include "llvm/Support/FileSystem.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 #include "llvm/Support/Path.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 #include "llvm/TextAPI/InterfaceFile.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 #include "llvm/TextAPI/TextAPIReader.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 using namespace llvm::MachO;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 using namespace llvm::opt;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 using namespace llvm::sys;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 // Create prefix string literals used in Options.td
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 #define PREFIX(NAME, VALUE) const char *NAME[] = VALUE;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 #include "Options.inc"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 #undef PREFIX
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 // Create table mapping all options defined in Options.td
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 static const OptTable::Info optInfo[] = {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 {X1, X2, X10, X11, OPT_##ID, Option::KIND##Class, \
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12},
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 #include "Options.inc"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 #undef OPTION
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 MachOOptTable::MachOOptTable() : OptTable(optInfo) {}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 // Set color diagnostics according to --color-diagnostics={auto,always,never}
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 // or --no-color-diagnostics flags.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 static void handleColorDiagnostics(InputArgList &args) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 const Arg *arg =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59 args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 OPT_no_color_diagnostics);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 if (!arg)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 if (arg->getOption().getID() == OPT_color_diagnostics) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 lld::errs().enable_colors(true);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66 lld::errs().enable_colors(false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 StringRef s = arg->getValue();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 if (s == "always")
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 lld::errs().enable_colors(true);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 else if (s == "never")
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 lld::errs().enable_colors(false);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73 else if (s != "auto")
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 error("unknown option: --color-diagnostics=" + s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79 // Make InputArgList from string vectors.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 unsigned missingIndex;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 unsigned missingCount;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 // Expand response files (arguments in the form of @<filename>)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85 // and then parse the argument again.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 cl::ExpandResponseFiles(saver, cl::TokenizeGNUCommandLine, vec);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 InputArgList args = ParseArgs(vec, missingIndex, missingCount);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 // Handle -fatal_warnings early since it converts missing argument warnings
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90 // to errors.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 if (missingCount)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 error(Twine(args.getArgString(missingIndex)) + ": missing argument");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96 handleColorDiagnostics(args);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 std::string nearest;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 if (findNearest(arg->getAsString(args), nearest) > 1)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101 error("unknown argument '" + arg->getAsString(args) + "'");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 error("unknown argument '" + arg->getAsString(args) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 "', did you mean '" + nearest + "'");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 return args;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 PrintHelp(lld::outs(), (std::string(argv0) + " [options] file...").c_str(),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111 "LLVM Linker", showHidden);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 lld::outs() << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 static std::string rewritePath(StringRef s) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 if (fs::exists(s))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 return relativeToRoot(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 return std::string(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 static std::string rewriteInputPath(StringRef s) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 // Don't bother rewriting "absolute" paths that are actually under the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 // syslibroot; simply rewriting the syslibroot is sufficient.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 if (rerootPath(s) == s && fs::exists(s))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 return relativeToRoot(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 return std::string(s);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129 // Reconstructs command line arguments so that so that you can re-run
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 // the same command with the same inputs. This is for --reproduce.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 std::string macho::createResponseFile(const InputArgList &args) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
132 SmallString<0> data;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
133 raw_svector_ostream os(data);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
134
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
135 // Copy the command line to the output while rewriting paths.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
136 for (const Arg *arg : args) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
137 switch (arg->getOption().getID()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
138 case OPT_reproduce:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
139 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
140 case OPT_INPUT:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
141 os << quote(rewriteInputPath(arg->getValue())) << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
142 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
143 case OPT_o:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
144 os << "-o " << quote(path::filename(arg->getValue())) << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
145 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
146 case OPT_filelist:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
147 if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
148 for (StringRef path : args::getLines(*buffer))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
149 os << quote(rewriteInputPath(path)) << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
150 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
151 case OPT_force_load:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
152 case OPT_weak_library:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
153 os << arg->getSpelling() << " "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
154 << quote(rewriteInputPath(arg->getValue())) << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
155 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
156 case OPT_F:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
157 case OPT_L:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
158 case OPT_bundle_loader:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
159 case OPT_exported_symbols_list:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
160 case OPT_order_file:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
161 case OPT_rpath:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
162 case OPT_syslibroot:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
163 case OPT_unexported_symbols_list:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
164 os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue()))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
165 << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
166 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
167 case OPT_sectcreate:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
168 os << arg->getSpelling() << " " << quote(arg->getValue(0)) << " "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
169 << quote(arg->getValue(1)) << " "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
170 << quote(rewritePath(arg->getValue(2))) << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
171 break;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
172 default:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
173 os << toString(*arg) << "\n";
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
174 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
175 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
176 return std::string(data.str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
177 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
178
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
179 Optional<std::string> macho::resolveDylibPath(StringRef path) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
180 // TODO: if a tbd and dylib are both present, we should check to make sure
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
181 // they are consistent.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
182 if (fs::exists(path))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
183 return std::string(path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
184 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
185 depTracker->logFileNotFound(path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
186
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
187 SmallString<261> location = path;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
188 path::replace_extension(location, ".tbd");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
189 if (fs::exists(location))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
190 return std::string(location);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
191 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
192 depTracker->logFileNotFound(location);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
193 return {};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
194 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
195
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
196 // It's not uncommon to have multiple attempts to load a single dylib,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
197 // especially if it's a commonly re-exported core library.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
198 static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
199
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
200 DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
201 bool isBundleLoader) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
202 CachedHashStringRef path(mbref.getBufferIdentifier());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
203 DylibFile *&file = loadedDylibs[path];
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
204 if (file)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
205 return file;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
206
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
207 DylibFile *newFile;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
208 file_magic magic = identify_magic(mbref.getBuffer());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
209 if (magic == file_magic::tapi_file) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
210 Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
211 if (!result) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
212 error("could not load TAPI file at " + mbref.getBufferIdentifier() +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
213 ": " + toString(result.takeError()));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
214 return nullptr;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
215 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
216 file = make<DylibFile>(**result, umbrella, isBundleLoader);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
217
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
218 // parseReexports() can recursively call loadDylib(). That's fine since
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
219 // we wrote the DylibFile we just loaded to the loadDylib cache via the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
220 // `file` reference. But the recursive load can grow loadDylibs, so the
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
221 // `file` reference might become invalid after parseReexports() -- so copy
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
222 // the pointer it refers to before continuing.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
223 newFile = file;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
224 if (newFile->exportingFile)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
225 newFile->parseReexports(**result);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
226 } else {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
227 assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
228 magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
229 magic == file_magic::macho_executable ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
230 magic == file_magic::macho_bundle);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
231 file = make<DylibFile>(mbref, umbrella, isBundleLoader);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
232
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
233 // parseLoadCommands() can also recursively call loadDylib(). See comment
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
234 // in previous block for why this means we must copy `file` here.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
235 newFile = file;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
236 if (newFile->exportingFile)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
237 newFile->parseLoadCommands(mbref);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
238 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
239 return newFile;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
240 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
241
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
242 Optional<StringRef>
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
243 macho::findPathCombination(const Twine &name,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
244 const std::vector<StringRef> &roots,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
245 ArrayRef<StringRef> extensions) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
246 SmallString<261> base;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
247 for (StringRef dir : roots) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
248 base = dir;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
249 path::append(base, name);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
250 for (StringRef ext : extensions) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
251 Twine location = base + ext;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
252 if (fs::exists(location))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
253 return saver.save(location.str());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
254 else
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
255 depTracker->logFileNotFound(location);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
256 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
257 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
258 return {};
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
259 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
260
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
261 StringRef macho::rerootPath(StringRef path) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
262 if (!path::is_absolute(path, path::Style::posix) || path.endswith(".o"))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
263 return path;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
264
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
265 if (Optional<StringRef> rerootedPath =
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
266 findPathCombination(path, config->systemLibraryRoots))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
267 return *rerootedPath;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
268
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
269 return path;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
270 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
271
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
272 Optional<InputFile *> macho::loadArchiveMember(MemoryBufferRef mb,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
273 uint32_t modTime,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
274 StringRef archiveName,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
275 bool objCOnly) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
276 if (config->zeroModTime)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
277 modTime = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
278
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
279 switch (identify_magic(mb.getBuffer())) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
280 case file_magic::macho_object:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
281 if (!objCOnly || hasObjCSection(mb))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
282 return make<ObjFile>(mb, modTime, archiveName);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
283 return None;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
284 case file_magic::bitcode:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
285 if (!objCOnly || check(isBitcodeContainingObjCCategory(mb)))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
286 return make<BitcodeFile>(mb);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
287 return None;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
288 default:
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
289 error(archiveName + ": archive member " + mb.getBufferIdentifier() +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
290 " has unhandled file type");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
291 return None;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
292 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
293 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
294
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
295 uint32_t macho::getModTime(StringRef path) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
296 if (config->zeroModTime)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
297 return 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
298
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
299 fs::file_status stat;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
300 if (!fs::status(path, stat))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
301 if (fs::exists(stat))
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
302 return toTimeT(stat.getLastModificationTime());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
303
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
304 warn("failed to get modification time of " + path);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
305 return 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
306 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
307
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
308 void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
309 if (config->printEachFile)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
310 message(toString(f));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
311 if (config->printWhyLoad)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
312 message(reason + " forced load of " + toString(f));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
313 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
314
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
315 macho::DependencyTracker::DependencyTracker(StringRef path)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
316 : path(path), active(!path.empty()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
317 if (active && fs::exists(path) && !fs::can_write(path)) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
318 warn("Ignoring dependency_info option since specified path is not "
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
319 "writeable.");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
320 active = false;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
321 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
322 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
323
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
324 void macho::DependencyTracker::write(llvm::StringRef version,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
325 const llvm::SetVector<InputFile *> &inputs,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
326 llvm::StringRef output) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
327 if (!active)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
328 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
329
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
330 std::error_code ec;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
331 llvm::raw_fd_ostream os(path, ec, llvm::sys::fs::OF_None);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
332 if (ec) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
333 warn("Error writing dependency info to file");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
334 return;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
335 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
336
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
337 auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
338 // XXX: Even though DepOpCode's underlying type is uint8_t,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
339 // this cast is still needed because Clang older than 10.x has a bug,
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
340 // where it doesn't know to cast the enum to its underlying type.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
341 // Hence `<< DepOpCode` is ambiguous to it.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
342 os << static_cast<uint8_t>(opcode);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
343 os << path;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
344 os << '\0';
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
345 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
346
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
347 addDep(DepOpCode::Version, version);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
348
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
349 // Sort the input by its names.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
350 std::vector<StringRef> inputNames;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
351 inputNames.reserve(inputs.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
352 for (InputFile *f : inputs)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
353 inputNames.push_back(f->getName());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
354 llvm::sort(inputNames);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
355
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
356 for (const StringRef &in : inputNames)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
357 addDep(DepOpCode::Input, in);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
358
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
359 for (const std::string &f : notFounds)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
360 addDep(DepOpCode::NotFound, f);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
361
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
362 addDep(DepOpCode::Output, output);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
363 }