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