LLVM 22.0.0git
LibraryScanner.cpp
Go to the documentation of this file.
1//===- LibraryScanner.cpp - Provide Library Scanning Implementation ----===//
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
11
13#include "llvm/Object/COFF.h"
14#include "llvm/Object/ELF.h"
17#include "llvm/Object/MachO.h"
20#include "llvm/Support/Error.h"
23#include "llvm/Support/Path.h"
27
28#ifdef LLVM_ON_UNIX
29#include <sys/stat.h>
30#include <unistd.h>
31#endif // LLVM_ON_UNIX
32
33#ifdef __APPLE__
34#include <sys/stat.h>
35#undef LC_LOAD_DYLIB
36#undef LC_RPATH
37#endif // __APPLE__
38
39#define DEBUG_TYPE "orc-scanner"
40
41namespace llvm::orc {
42
43void handleError(Error Err, StringRef context = "") {
44 consumeError(handleErrors(std::move(Err), [&](const ErrorInfoBase &EIB) {
45 dbgs() << "LLVM Error";
46 if (!context.empty())
47 dbgs() << " [" << context << "]";
48 dbgs() << ": " << EIB.message() << "\n";
49 }));
50}
51
53 static const llvm::Triple HostTriple(llvm::sys::getProcessTriple());
54
55 if (HostTriple.getArch() != Obj.getArch())
56 return false;
57
58 if (Obj.getTripleObjectFormat() != HostTriple.getObjectFormat())
59 return false;
60
61 return true;
62}
63
65ObjectFileLoader::loadObjectFileWithOwnership(StringRef FilePath) {
66 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Attempting to open file " << FilePath
67 << "\n";);
68 if (auto ObjOrErr = object::ObjectFile::createObjectFile(FilePath)) {
69
70 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Detected object file\n";);
71
72 auto OwningBin = std::move(*ObjOrErr);
73
74 if (!isArchitectureCompatible(*OwningBin.getBinary())) {
75 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Incompatible architecture: "
76 << FilePath << "\n";);
78 "Incompatible object file: %s",
79 FilePath.str().c_str());
80 }
81
82 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Object file is compatible\n";);
83
84 return std::move(OwningBin);
85 } else {
86#if defined(__APPLE__)
87 consumeError(ObjOrErr.takeError());
88 auto BinOrErr = object::createBinary(FilePath);
89 if (!BinOrErr) {
90 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Failed to open file " << FilePath
91 << "\n";);
92 return BinOrErr.takeError();
93 }
94
95 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Successfully opened file "
96 << FilePath << "\n";);
97
98 auto OwningBin = BinOrErr->takeBinary();
99 object::Binary *Bin = OwningBin.first.get();
100
101 if (Bin->isArchive()) {
103 dbgs() << "ObjectFileLoader: File is an archive, not supported: "
104 << FilePath << "\n";);
105 return createStringError(std::errc::invalid_argument,
106 "Archive files are not supported: %s",
107 FilePath.str().c_str());
108 }
109
112 dbgs() << "ObjectFileLoader: Detected Mach-O universal binary: "
113 << FilePath << "\n";);
114 for (auto ObjForArch : UB->objects()) {
115 auto ObjOrErr = ObjForArch.getAsObjectFile();
116 if (!ObjOrErr) {
117 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Skipping invalid "
118 "architecture slice\n";);
119
120 consumeError(ObjOrErr.takeError());
121 continue;
122 }
123
124 std::unique_ptr<object::ObjectFile> Obj = std::move(ObjOrErr.get());
125 if (isArchitectureCompatible(*Obj)) {
127 dbgs() << "ObjectFileLoader: Found compatible object slice\n";);
128
129 return object::OwningBinary<object::ObjectFile>(
130 std::move(Obj), std::move(OwningBin.second));
131
132 } else {
133 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Incompatible architecture "
134 "slice skipped\n";);
135 }
136 }
137 LLVM_DEBUG(dbgs() << "ObjectFileLoader: No compatible slices found in "
138 "universal binary\n";);
140 "No compatible object found in fat binary: %s",
141 FilePath.str().c_str());
142 }
143 return ObjOrErr.takeError();
144#else
145 LLVM_DEBUG(dbgs() << "ObjectFileLoader: Failed to open file " << FilePath
146 << "\n";);
147 return ObjOrErr.takeError();
148#endif
149 }
150
152 "Not a compatible object file : %s",
153 FilePath.str().c_str());
154}
155
156template <class ELFT>
158 if (ELFObj.getHeader().e_type != ELF::ET_DYN)
159 return false;
160
161 auto PHOrErr = ELFObj.program_headers();
162 if (!PHOrErr) {
163 consumeError(PHOrErr.takeError());
164 return true;
165 }
166
167 for (auto Phdr : *PHOrErr) {
168 if (Phdr.p_type == ELF::PT_INTERP)
169 return false;
170 }
171
172 return true;
173}
174
176 if (Obj.isELF()) {
177 if (auto *ELF32LE = dyn_cast<object::ELF32LEObjectFile>(&Obj))
178 return isELFSharedLibrary(ELF32LE->getELFFile());
179 if (auto *ELF64LE = dyn_cast<object::ELF64LEObjectFile>(&Obj))
180 return isELFSharedLibrary(ELF64LE->getELFFile());
181 if (auto *ELF32BE = dyn_cast<object::ELF32BEObjectFile>(&Obj))
182 return isELFSharedLibrary(ELF32BE->getELFFile());
183 if (auto *ELF64BE = dyn_cast<object::ELF64BEObjectFile>(&Obj))
184 return isELFSharedLibrary(ELF64BE->getELFFile());
185 } else if (Obj.isMachO()) {
188 if (!MachO) {
189 LLVM_DEBUG(dbgs() << "Failed to cast to MachOObjectFile.\n";);
190 return false;
191 }
192 LLVM_DEBUG({
193 bool Result =
194 MachO->getHeader().filetype == MachO::HeaderFileType::MH_DYLIB;
195 dbgs() << "Mach-O filetype: " << MachO->getHeader().filetype
196 << " (MH_DYLIB == " << MachO::HeaderFileType::MH_DYLIB
197 << "), shared: " << Result << "\n";
198 });
199
200 return MachO->getHeader().filetype == MachO::HeaderFileType::MH_DYLIB;
201 } else if (Obj.isCOFF()) {
203 if (!coff)
204 return false;
206 } else {
207 LLVM_DEBUG(dbgs() << "Binary is not an ObjectFile.\n";);
208 }
209
210 return false;
211}
212
214 LLVM_DEBUG(dbgs() << "Checking if path is a shared library: " << Path
215 << "\n";);
216
217 auto FileType = sys::fs::get_file_type(Path, /*Follow*/ true);
218 if (FileType != sys::fs::file_type::regular_file) {
219 LLVM_DEBUG(dbgs() << "File type is not a regular file for path: " << Path
220 << "\n";);
221 return false;
222 }
223
224 file_magic MagicCode;
225 identify_magic(Path, MagicCode);
226
227 // Skip archives.
228 if (MagicCode == file_magic::archive)
229 return false;
230
231 // Object file inspection for PE/COFF, ELF, and Mach-O
232 bool NeedsObjectInspection =
233#if defined(_WIN32)
234 (MagicCode == file_magic::pecoff_executable);
235#elif defined(__APPLE__)
240#elif defined(LLVM_ON_UNIX)
241#ifdef __CYGWIN__
242 (MagicCode == file_magic::pecoff_executable);
243#else
244 (MagicCode == file_magic::elf_shared_object);
245#endif
246#else
247#error "Unsupported platform."
248#endif
249
250 if (!NeedsObjectInspection) {
251 LLVM_DEBUG(dbgs() << "Path is not identified as a shared library: " << Path
252 << "\n";);
253 return false;
254 }
255
256 ObjectFileLoader ObjLoader(Path);
257 auto ObjOrErr = ObjLoader.getObjectFile();
258 if (!ObjOrErr) {
259 consumeError(ObjOrErr.takeError());
260 return false;
261 }
262
263 bool IsShared = isSharedLibraryObject(ObjOrErr.get());
264
265 if (IsShared && ObjCache)
266 ObjCache->insert(Path, std::move(ObjLoader));
267
268 return IsShared;
269}
270
272 SmallString<512> ExecPath(sys::fs::getMainExecutable(nullptr, nullptr));
274
275 SmallString<512> LoaderDir;
276 if (LoaderPath.empty()) {
277 LoaderDir = ExecPath;
278 } else {
279 LoaderDir = LoaderPath.str();
280 if (!sys::fs::is_directory(LoaderPath))
282 }
283
284#ifdef __APPLE__
285 Placeholders.push_back({"@loader_path", std::string(LoaderDir)});
286 Placeholders.push_back({"@executable_path", std::string(ExecPath)});
287#else
288 Placeholders.push_back({"$origin", std::string(LoaderDir)});
289#endif
290}
291
292std::optional<std::string>
294 DylibPathValidator &Validator) const {
295 for (const auto &SP : Paths) {
296 std::string Base = Subst.substitute(SP);
297
298 SmallString<512> FullPath(Base);
299 if (!PlaceholderPrefix.empty() &&
300 Stem.starts_with_insensitive(PlaceholderPrefix))
301 FullPath.append(Stem.drop_front(PlaceholderPrefix.size()));
302 else
303 sys::path::append(FullPath, Stem);
304
305 LLVM_DEBUG(dbgs() << "SearchPathResolver::resolve FullPath = " << FullPath
306 << "\n";);
307
308 if (auto Valid = Validator.validate(FullPath.str()))
309 return Valid;
310 }
311
312 return std::nullopt;
313}
314
315std::optional<std::string>
316DylibResolverImpl::tryWithExtensions(StringRef LibStem) const {
317 LLVM_DEBUG(dbgs() << "tryWithExtensions: baseName = " << LibStem << "\n";);
318 SmallVector<SmallString<256>, 8> Candidates;
319
320 // Add extensions by platform
321#if defined(__APPLE__)
322 Candidates.emplace_back(LibStem);
323 Candidates.back() += ".dylib";
324#elif defined(_WIN32)
325 Candidates.emplace_back(LibStem);
326 Candidates.back() += ".dll";
327#else
328 Candidates.emplace_back(LibStem);
329 Candidates.back() += ".so";
330#endif
331
332 // Optionally try "lib" prefix if not already there
333 StringRef FileName = sys::path::filename(LibStem);
335 if (!FileName.starts_with("lib")) {
336 SmallString<256> WithPrefix(Base);
337 if (!WithPrefix.empty())
338 sys::path::append(WithPrefix, ""); // ensure separator if needed
339 WithPrefix += "lib";
340 WithPrefix += FileName;
341
342#if defined(__APPLE__)
343 WithPrefix += ".dylib";
344#elif defined(_WIN32)
345 WithPrefix += ".dll";
346#else
347 WithPrefix += ".so";
348#endif
349
350 Candidates.push_back(std::move(WithPrefix));
351 }
352
353 LLVM_DEBUG({
354 dbgs() << " Candidates to try:\n";
355 for (const auto &C : Candidates)
356 dbgs() << " " << C << "\n";
357 });
358
359 // Try all variants using tryAllPaths
360 for (const auto &Name : Candidates) {
361
362 LLVM_DEBUG(dbgs() << " Trying candidate: " << Name << "\n";);
363
364 for (const auto &R : Resolvers) {
365 if (auto Res = R.resolve(Name, Substitutor, Validator))
366 return Res;
367 }
368 }
369
370 LLVM_DEBUG(dbgs() << " -> No candidate Resolved.\n";);
371
372 return std::nullopt;
373}
374
375std::optional<std::string>
376DylibResolverImpl::resolve(StringRef LibStem, bool VariateLibStem) const {
377 LLVM_DEBUG(dbgs() << "Resolving library stem: " << LibStem << "\n";);
378
379 // If it is an absolute path, don't try iterate over the paths.
380 if (sys::path::is_absolute(LibStem)) {
381 LLVM_DEBUG(dbgs() << " -> Absolute path detected.\n";);
382 return Validator.validate(LibStem);
383 }
384
385 if (!LibStem.starts_with_insensitive("@rpath")) {
386 if (auto norm = Validator.validate(Substitutor.substitute(LibStem))) {
387 LLVM_DEBUG(dbgs() << " -> Resolved after substitution: " << *norm
388 << "\n";);
389
390 return norm;
391 }
392 }
393
394 for (const auto &R : Resolvers) {
395 LLVM_DEBUG(dbgs() << " -> Resolving via search path ... \n";);
396 if (auto Result = R.resolve(LibStem, Substitutor, Validator)) {
397 LLVM_DEBUG(dbgs() << " -> Resolved via search path: " << *Result
398 << "\n";);
399
400 return Result;
401 }
402 }
403
404 // Expand libStem with paths, extensions, etc.
405 // std::string foundName;
406 if (VariateLibStem) {
407 LLVM_DEBUG(dbgs() << " -> Trying with extensions...\n";);
408
409 if (auto Norm = tryWithExtensions(LibStem)) {
410 LLVM_DEBUG(dbgs() << " -> Resolved via tryWithExtensions: " << *Norm
411 << "\n";);
412 return Norm;
413 }
414 }
415
416 LLVM_DEBUG(dbgs() << " -> Could not resolve: " << LibStem << "\n";);
417
418 return std::nullopt;
419}
420
421#ifndef _WIN32
423 // If already cached - retun cached result
424 if (auto Cache = LibPathCache->read_lstat(Path))
425 return *Cache;
426
427 // Not cached: perform lstat and store
428 struct stat buf{};
429 mode_t st_mode = (lstat(Path.str().c_str(), &buf) == -1) ? 0 : buf.st_mode;
430
431 LibPathCache->insert_lstat(Path, st_mode);
432
433 return st_mode;
434}
435
436std::optional<std::string> PathResolver::readlinkCached(StringRef Path) {
437 // If already cached - retun cached result
438 if (auto Cache = LibPathCache->read_link(Path))
439 return Cache;
440
441 // If result not in cache - call system function and cache result
442 char buf[PATH_MAX];
443 ssize_t len;
444 if ((len = readlink(Path.str().c_str(), buf, sizeof(buf))) != -1) {
445 buf[len] = '\0';
446 std::string s(buf);
447 LibPathCache->insert_link(Path, s);
448 return s;
449 }
450 return std::nullopt;
451}
452
453void createComponent(StringRef Path, StringRef BasePath, bool BaseIsResolved,
454 SmallVector<StringRef, 16> &Component) {
456 if (!BaseIsResolved) {
457 if (Path[0] == '~' &&
458 (Path.size() == 1 || sys::path::is_separator(Path[1]))) {
459 static SmallString<128> HomeP;
460 if (HomeP.str().empty())
462 StringRef(HomeP).split(Component, Separator, /*MaxSplit*/ -1,
463 /*KeepEmpty*/ false);
464 } else if (BasePath.empty()) {
465 static SmallString<256> CurrentPath;
466 if (CurrentPath.str().empty())
467 sys::fs::current_path(CurrentPath);
468 StringRef(CurrentPath)
469 .split(Component, Separator, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
470 } else {
471 BasePath.split(Component, Separator, /*MaxSplit*/ -1,
472 /*KeepEmpty*/ false);
473 }
474 }
475
476 Path.split(Component, Separator, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
477}
478
480 SmallVector<StringRef, 16> NormalizedPath;
481 for (auto &Part : PathParts) {
482 if (Part == ".") {
483 continue;
484 } else if (Part == "..") {
485 if (!NormalizedPath.empty() && NormalizedPath.back() != "..") {
486 NormalizedPath.pop_back();
487 } else {
488 NormalizedPath.push_back("..");
489 }
490 } else {
491 NormalizedPath.push_back(Part);
492 }
493 }
494 PathParts.swap(NormalizedPath);
495}
496#endif
497
498std::optional<std::string> PathResolver::realpathCached(StringRef Path,
499 std::error_code &EC,
501 bool BaseIsResolved,
502 long SymLoopLevel) {
503 EC.clear();
504
505 if (Path.empty()) {
506 EC = std::make_error_code(std::errc::no_such_file_or_directory);
507 LLVM_DEBUG(dbgs() << "PathResolver::realpathCached: Empty path\n";);
508
509 return std::nullopt;
510 }
511
512 if (SymLoopLevel <= 0) {
513 EC = std::make_error_code(std::errc::too_many_symbolic_link_levels);
515 dbgs() << "PathResolver::realpathCached: Too many Symlink levels: "
516 << Path << "\n";);
517
518 return std::nullopt;
519 }
520
521 // If already cached - retun cached result
522 bool isRelative = sys::path::is_relative(Path);
523 if (!isRelative) {
524 if (auto Cached = LibPathCache->read_realpath(Path)) {
525 EC = Cached->ErrnoCode;
526 if (EC) {
527 LLVM_DEBUG(dbgs() << "PathResolver::realpathCached: Cached (error) for "
528 << Path << "\n";);
529 } else {
531 dbgs() << "PathResolver::realpathCached: Cached (success) for "
532 << Path << " => " << Cached->canonicalPath << "\n";);
533 }
534 return Cached->canonicalPath.empty()
535 ? std::nullopt
536 : std::make_optional(Cached->canonicalPath);
537 }
538 }
539
540 LLVM_DEBUG(dbgs() << "PathResolver::realpathCached: Resolving path: " << Path
541 << "\n";);
542
543 // If result not in cache - call system function and cache result
544
546 SmallString<256> Resolved(Separator);
547#ifndef _WIN32
549
550 if (isRelative) {
551 if (BaseIsResolved) {
552 Resolved.assign(Base);
553 LLVM_DEBUG(dbgs() << " Using Resolved base: " << Base << "\n";);
554 }
555 createComponent(Path, Base, BaseIsResolved, Components);
556 } else {
557 Path.split(Components, Separator, /*MaxSplit*/ -1, /*KeepEmpty*/ false);
558 }
559
560 normalizePathSegments(Components);
561 LLVM_DEBUG({
562 for (auto &C : Components)
563 dbgs() << " " << C << " ";
564
565 dbgs() << "\n";
566 });
567
568 // Handle path list items
569 for (const auto &Component : Components) {
570 if (Component == ".")
571 continue;
572 if (Component == "..") {
573 // collapse "a/b/../c" to "a/c"
574 size_t S = Resolved.rfind(Separator);
575 if (S != llvm::StringRef::npos)
576 Resolved.resize(S);
577 if (Resolved.empty())
578 Resolved = Separator;
579 continue;
580 }
581
582 size_t oldSize = Resolved.size();
583 sys::path::append(Resolved, Component);
584 const char *ResolvedPath = Resolved.c_str();
585 LLVM_DEBUG(dbgs() << " Processing Component: " << Component << " => "
586 << ResolvedPath << "\n";);
587 mode_t st_mode = lstatCached(ResolvedPath);
588
589 if (S_ISLNK(st_mode)) {
590 LLVM_DEBUG(dbgs() << " Found symlink: " << ResolvedPath << "\n";);
591
592 auto SymlinkOpt = readlinkCached(ResolvedPath);
593 if (!SymlinkOpt) {
594 EC = std::make_error_code(std::errc::no_such_file_or_directory);
595 LibPathCache->insert_realpath(Path, LibraryPathCache::PathInfo{"", EC});
596 LLVM_DEBUG(dbgs() << " Failed to read symlink: " << ResolvedPath
597 << "\n";);
598
599 return std::nullopt;
600 }
601
602 StringRef Symlink = *SymlinkOpt;
603 LLVM_DEBUG(dbgs() << " Symlink points to: " << Symlink << "\n";);
604
605 std::string resolvedBase = "";
606 if (sys::path::is_relative(Symlink)) {
607 Resolved.resize(oldSize);
608 resolvedBase = Resolved.str().str();
609 }
610
611 auto RealSymlink =
612 realpathCached(Symlink, EC, resolvedBase,
613 /*BaseIsResolved=*/true, SymLoopLevel - 1);
614 if (!RealSymlink) {
615 LibPathCache->insert_realpath(Path, LibraryPathCache::PathInfo{"", EC});
616 LLVM_DEBUG(dbgs() << " Failed to resolve symlink target: " << Symlink
617 << "\n";);
618
619 return std::nullopt;
620 }
621
622 Resolved.assign(*RealSymlink);
623 LLVM_DEBUG(dbgs() << " Symlink Resolved to: " << Resolved << "\n";);
624
625 } else if (st_mode == 0) {
626 EC = std::make_error_code(std::errc::no_such_file_or_directory);
627 LibPathCache->insert_realpath(Path, LibraryPathCache::PathInfo{"", EC});
628 LLVM_DEBUG(dbgs() << " Component does not exist: " << ResolvedPath
629 << "\n";);
630
631 return std::nullopt;
632 }
633 }
634#else
635 EC = sys::fs::real_path(Path, Resolved); // Windows fallback
636#endif
637
638 std::string Canonical = Resolved.str().str();
639 {
640 LibPathCache->insert_realpath(Path, LibraryPathCache::PathInfo{
641 Canonical,
642 std::error_code() // success
643 });
644 }
645 LLVM_DEBUG(dbgs() << "PathResolver::realpathCached: Final Resolved: " << Path
646 << " => " << Canonical << "\n";);
647 return Canonical;
648}
649
650void LibraryScanHelper::addBasePath(const std::string &Path, PathType K) {
651 std::error_code EC;
652 std::string Canon = resolveCanonical(Path, EC);
653 if (EC) {
655 dbgs()
656 << "LibraryScanHelper::addBasePath: Failed to canonicalize path: "
657 << Path << "\n";);
658 return;
659 }
660 std::unique_lock<std::shared_mutex> Lock(Mtx);
661 if (LibSearchPaths.count(Canon)) {
662 LLVM_DEBUG(dbgs() << "LibraryScanHelper::addBasePath: Already added: "
663 << Canon << "\n";);
664 return;
665 }
666 K = K == PathType::Unknown ? classifyKind(Canon) : K;
667 LibSearchPaths[Canon] = std::make_unique<LibrarySearchPath>(Canon, K);
668 auto &SP = LibSearchPaths[Canon];
669
670 if (K == PathType::User) {
671 LLVM_DEBUG(dbgs() << "LibraryScanHelper::addBasePath: Added User path: "
672 << Canon << "\n";);
673 UnscannedUsr.push_back(StringRef(SP->BasePath));
674 } else {
675 LLVM_DEBUG(dbgs() << "LibraryScanHelper::addBasePath: Added System path: "
676 << Canon << "\n";);
677 UnscannedSys.push_back(StringRef(SP->BasePath));
678 }
679}
680
682 PathType K, size_t BatchSize,
684 auto &Queue = (K == PathType::User) ? UnscannedUsr : UnscannedSys;
685
686 std::unique_lock<std::shared_mutex> Lock(Mtx);
687
688 while (!Queue.empty() && (BatchSize == 0 || Result.size() < BatchSize)) {
689 StringRef Base = Queue.front();
690 auto It = LibSearchPaths.find(Base);
691 if (It != LibSearchPaths.end()) {
692 auto &SP = It->second;
694 if (SP->State.compare_exchange_strong(Expected, ScanState::Scanning)) {
695 Result.push_back(SP.get());
696 }
697 }
698 Queue.pop_front();
699 }
700}
701
703 std::error_code EC;
704 std::string Canon = resolveCanonical(Path, EC);
705 if (EC)
706 return false;
707
708 std::shared_lock<std::shared_mutex> Lock(Mtx);
709 return LibSearchPaths.count(Canon) > 0;
710}
711
713 std::shared_lock<std::shared_mutex> Lock(Mtx);
714 for (const auto &KV : LibSearchPaths) {
715 const auto &SP = KV.second;
716 if (SP->Kind == K && SP->State == ScanState::NotScanned)
717 return true;
718 }
719 return false;
720}
721
723 std::shared_lock<std::shared_mutex> Lock(Mtx);
724
725 for (auto &[_, SP] : LibSearchPaths) {
727
728 if (!SP->State.compare_exchange_strong(Expected, ScanState::NotScanned))
729 continue;
730
731 auto &TargetList =
732 (SP->Kind == PathType::User) ? UnscannedUsr : UnscannedSys;
733 TargetList.emplace_back(SP->BasePath);
734 }
735}
736
737std::string LibraryScanHelper::resolveCanonical(StringRef Path,
738 std::error_code &EC) const {
739 auto Canon = LibPathResolver->resolve(Path, EC);
740 return EC ? Path.str() : *Canon;
741}
742
743PathType LibraryScanHelper::classifyKind(StringRef Path) const {
744 // Detect home directory
745 const char *Home = getenv("HOME");
746 if (Home && Path.starts_with(Home))
747 return PathType::User;
748
749 static const std::array<std::string, 5> UserPrefixes = {
750 "/usr/local", // often used by users for manual installs
751 "/opt/homebrew", // common on macOS
752 "/opt/local", // MacPorts
753 "/home", // Linux home dirs
754 "/Users", // macOS user dirs
755 };
756
757 for (const auto &Prefix : UserPrefixes) {
758 if (Path.starts_with(Prefix))
759 return PathType::User;
760 }
761
762 return PathType::System;
763}
764
766 LibraryDepsInfo Libdeps;
767 LLVM_DEBUG(dbgs() << "Parsing Mach-O dependencies...\n";);
768 for (const auto &Command : Obj.load_commands()) {
769 switch (Command.C.cmd) {
770 case MachO::LC_LOAD_DYLIB: {
771 MachO::dylib_command dylibCmd = Obj.getDylibIDLoadCommand(Command);
772 const char *name = Command.Ptr + dylibCmd.dylib.name;
773 Libdeps.addDep(name);
774 LLVM_DEBUG(dbgs() << " Found LC_LOAD_DYLIB: " << name << "\n";);
775 } break;
776 case MachO::LC_LOAD_WEAK_DYLIB:
777 case MachO::LC_REEXPORT_DYLIB:
778 case MachO::LC_LOAD_UPWARD_DYLIB:
779 case MachO::LC_LAZY_LOAD_DYLIB:
780 break;
781 case MachO::LC_RPATH: {
782 // Extract RPATH
783 MachO::rpath_command rpathCmd = Obj.getRpathCommand(Command);
784 const char *rpath = Command.Ptr + rpathCmd.path;
785 LLVM_DEBUG(dbgs() << " Found LC_RPATH: " << rpath << "\n";);
786
788 SplitString(StringRef(rpath), RawPaths,
789 sys::EnvPathSeparator == ':' ? ":" : ";");
790
791 for (const auto &raw : RawPaths) {
792 Libdeps.addRPath(raw.str()); // Convert to std::string
793 LLVM_DEBUG(dbgs() << " Parsed RPATH entry: " << raw << "\n";);
794 }
795 break;
796 }
797 }
798 }
799
800 return Expected<LibraryDepsInfo>(std::move(Libdeps));
801}
802
803template <class ELFT>
805 auto DynamicEntriesOrError = Elf.dynamicEntries();
806 if (!DynamicEntriesOrError)
807 return DynamicEntriesOrError.takeError();
808
809 for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
810 if (Dyn.d_tag == ELF::DT_STRTAB) {
811 auto MappedAddrOrError = Elf.toMappedAddr(Dyn.getPtr());
812 if (!MappedAddrOrError)
813 return MappedAddrOrError.takeError();
814 return StringRef(reinterpret_cast<const char *>(*MappedAddrOrError));
815 }
816 }
817
818 // If the dynamic segment is not present, we fall back on the sections.
819 auto SectionsOrError = Elf.sections();
820 if (!SectionsOrError)
821 return SectionsOrError.takeError();
822
823 for (const typename ELFT::Shdr &Sec : *SectionsOrError) {
824 if (Sec.sh_type == ELF::SHT_DYNSYM)
825 return Elf.getStringTableForSymtab(Sec);
826 }
827
828 return make_error<StringError>("dynamic string table not found",
830}
831
832template <typename ELFT>
834 LibraryDepsInfo Deps;
835 Expected<StringRef> StrTabOrErr = getDynamicStrTab(Elf);
836 if (!StrTabOrErr)
837 return StrTabOrErr.takeError();
838
839 const char *Data = StrTabOrErr->data();
840
841 auto DynamicEntriesOrError = Elf.dynamicEntries();
842 if (!DynamicEntriesOrError) {
843 return DynamicEntriesOrError.takeError();
844 }
845
846 for (const typename ELFT::Dyn &Dyn : *DynamicEntriesOrError) {
847 switch (Dyn.d_tag) {
848 case ELF::DT_NEEDED:
849 Deps.addDep(Data + Dyn.d_un.d_val);
850 break;
851 case ELF::DT_RPATH: {
853 SplitString(Data + Dyn.d_un.d_val, RawPaths,
854 sys::EnvPathSeparator == ':' ? ":" : ";");
855 for (const auto &raw : RawPaths)
856 Deps.addRPath(raw.str());
857 break;
858 }
859 case ELF::DT_RUNPATH: {
861 SplitString(Data + Dyn.d_un.d_val, RawPaths,
862 sys::EnvPathSeparator == ':' ? ":" : ";");
863 for (const auto &raw : RawPaths)
864 Deps.addRunPath(raw.str());
865 break;
866 }
867 case ELF::DT_FLAGS_1:
868 // Check if this is not a pie executable.
869 if (Dyn.d_un.d_val & ELF::DF_1_PIE)
870 Deps.isPIE = true;
871 break;
872 // (Dyn.d_tag == ELF::DT_NULL) continue;
873 // (Dyn.d_tag == ELF::DT_AUXILIARY || Dyn.d_tag == ELF::DT_FILTER)
874 default:
875 break;
876 }
877 }
878
879 return Expected<LibraryDepsInfo>(std::move(Deps));
880}
881
883 using namespace object;
884 LLVM_DEBUG(dbgs() << "parseELFDeps: Detected ELF object\n";);
885 if (const auto *ELF = dyn_cast<ELF32LEObjectFile>(&Obj))
886 return parseELF(ELF->getELFFile());
887 else if (const auto *ELF = dyn_cast<ELF32BEObjectFile>(&Obj))
888 return parseELF(ELF->getELFFile());
889 else if (const auto *ELF = dyn_cast<ELF64LEObjectFile>(&Obj))
890 return parseELF(ELF->getELFFile());
891 else if (const auto *ELF = dyn_cast<ELF64BEObjectFile>(&Obj))
892 return parseELF(ELF->getELFFile());
893
894 LLVM_DEBUG(dbgs() << "parseELFDeps: Unknown ELF format\n";);
895 return createStringError(std::errc::not_supported, "Unknown ELF format");
896}
897
899 object::ObjectFile *Obj) {
900
901 if (auto *elfObj = dyn_cast<object::ELFObjectFileBase>(Obj)) {
902 LLVM_DEBUG(dbgs() << "extractDeps: File " << FilePath
903 << " is an ELF object\n";);
904
905 return parseELFDeps(*elfObj);
906 }
907
908 if (auto *macho = dyn_cast<object::MachOObjectFile>(Obj)) {
909 LLVM_DEBUG(dbgs() << "extractDeps: File " << FilePath
910 << " is a Mach-O object\n";);
911 return parseMachODeps(*macho);
912 }
913
914 if (Obj->isCOFF()) {
915 // TODO: COFF support
916 return LibraryDepsInfo();
917 }
918
919 LLVM_DEBUG(dbgs() << "extractDeps: Unsupported binary format for file "
920 << FilePath << "\n";);
922 "Unsupported binary format: %s",
923 FilePath.str().c_str());
924}
925
926Expected<LibraryDepsInfo> LibraryScanner::extractDeps(StringRef FilePath) {
927 LLVM_DEBUG(dbgs() << "extractDeps: Attempting to open file " << FilePath
928 << "\n";);
929 // check cache first
930 if (auto Cached = ObjCache.take(FilePath)) {
931 auto ObjOrErr = Cached->getObjectFile();
932 if (!ObjOrErr)
933 return ObjOrErr.takeError();
934 return parseDependencies(FilePath, &*ObjOrErr);
935 }
936
937 // fall back to normal loading
938 ObjectFileLoader ObjLoader(FilePath);
939 auto ObjOrErr = ObjLoader.getObjectFile();
940 if (!ObjOrErr) {
941 LLVM_DEBUG(dbgs() << "extractDeps: Failed to open " << FilePath << "\n";);
942 return ObjOrErr.takeError();
943 }
944
945 return parseDependencies(FilePath, &*ObjOrErr);
946}
947
948bool LibraryScanner::shouldScan(StringRef FilePath, bool IsResolvingDep) {
949 LLVM_DEBUG(dbgs() << "[shouldScan] Checking: " << FilePath << "\n";);
950
951 LibraryPathCache &Cache = ScanHelper.getCache();
952 // [1] Skip if we've already seen this path (via cache)
953 if (Cache.hasSeen(FilePath)) {
954 LLVM_DEBUG(dbgs() << " -> Skipped: already seen.\n";);
955 return false;
956 }
957
958 // [2] Already tracked in LibraryManager?
959 /*if (LibMgr.hasLibrary(FilePath)) {
960 LLVM_DEBUG(dbgs() << " -> Skipped: already tracked by LibraryManager.\n";);
961 return false;
962 }*/
963
964 // [3] Skip if it's not a shared library.
965 if (!IsResolvingDep && !Validator.isSharedLibrary(FilePath)) {
966 LLVM_DEBUG(dbgs() << " -> Skipped: not a shared library.\n";);
967 return false;
968 }
969
970 // Mark seen this path
971 Cache.markSeen(FilePath.str());
972
973 // [4] Run user-defined hook (default: always true)
974 if (!ShouldScanCall(FilePath)) {
975 LLVM_DEBUG(dbgs() << " -> Skipped: user-defined hook rejected.\n";);
976 return false;
977 }
978
979 LLVM_DEBUG(dbgs() << " -> Accepted: ready to scan " << FilePath << "\n";);
980 return true;
981}
982
983void LibraryScanner::handleLibrary(StringRef FilePath, PathType K, int level) {
984 LLVM_DEBUG(dbgs() << "LibraryScanner::handleLibrary: Scanning: " << FilePath
985 << ", level=" << level << "\n";);
986 if (!shouldScan(FilePath, level > 0)) {
987 LLVM_DEBUG(dbgs() << " Skipped (shouldScan returned false): " << FilePath
988 << "\n";);
989 return;
990 }
991
992 auto DepsOrErr = extractDeps(FilePath);
993 if (!DepsOrErr) {
994 LLVM_DEBUG(dbgs() << " Failed to extract deps for: " << FilePath << "\n";);
995 handleError(DepsOrErr.takeError());
996 return;
997 }
998
999 LibraryDepsInfo &Deps = *DepsOrErr;
1000
1001 LLVM_DEBUG({
1002 dbgs() << " Found deps : \n";
1003 for (const auto &dep : Deps.deps)
1004 dbgs() << " : " << dep << "\n";
1005 dbgs() << " Found @rpath : " << Deps.rpath.size() << "\n";
1006 for (const auto &r : Deps.rpath)
1007 dbgs() << " : " << r << "\n";
1008 dbgs() << " Found @runpath : \n";
1009 for (const auto &r : Deps.runPath)
1010 dbgs() << " : " << r << "\n";
1011 });
1012
1013 if (Deps.isPIE && level == 0) {
1014 LLVM_DEBUG(dbgs() << " Skipped PIE executable at top level: " << FilePath
1015 << "\n";);
1016
1017 return;
1018 }
1019
1020 bool Added = LibMgr.addLibrary(FilePath.str(), K);
1021 if (!Added) {
1022 LLVM_DEBUG(dbgs() << " Already added: " << FilePath << "\n";);
1023 return;
1024 }
1025
1026 // Heuristic 1: No RPATH/RUNPATH, skip deps
1027 if (Deps.rpath.empty() && Deps.runPath.empty()) {
1028 LLVM_DEBUG(
1029 dbgs() << "LibraryScanner::handleLibrary: Skipping deps (Heuristic1): "
1030 << FilePath << "\n";);
1031 return;
1032 }
1033
1034 // Heuristic 2: All RPATH and RUNPATH already tracked
1035 auto allTracked = [&](const auto &Paths) {
1036 LLVM_DEBUG(dbgs() << " Checking : " << Paths.size() << "\n";);
1037 return std::all_of(Paths.begin(), Paths.end(), [&](StringRef P) {
1038 LLVM_DEBUG(dbgs() << " Checking isTrackedBasePath : " << P << "\n";);
1039 return ScanHelper.isTrackedBasePath(
1040 DylibResolver::resolvelinkerFlag(P, FilePath));
1041 });
1042 };
1043
1044 if (allTracked(Deps.rpath) && allTracked(Deps.runPath)) {
1045 LLVM_DEBUG(
1046 dbgs() << "LibraryScanner::handleLibrary: Skipping deps (Heuristic2): "
1047 << FilePath << "\n";);
1048 return;
1049 }
1050
1051 DylibResolver Resolver(Validator);
1052 Resolver.configure(FilePath,
1053 {{Deps.rpath, SearchPathType::RPath},
1054 {ScanHelper.getSearchPaths(), SearchPathType::UsrOrSys},
1055 {Deps.runPath, SearchPathType::RunPath}});
1056 for (StringRef Dep : Deps.deps) {
1057 LLVM_DEBUG(dbgs() << " Resolving dep: " << Dep << "\n";);
1058 auto DepFullOpt = Resolver.resolve(Dep);
1059 if (!DepFullOpt) {
1060 LLVM_DEBUG(dbgs() << " Failed to resolve dep: " << Dep << "\n";);
1061 continue;
1062 }
1063 LLVM_DEBUG(dbgs() << " Resolved dep to: " << *DepFullOpt << "\n";);
1064
1065 handleLibrary(*DepFullOpt, K, level + 1);
1066 }
1067}
1068
1069void LibraryScanner::scanBaseDir(LibrarySearchPath *SP) {
1070 if (!sys::fs::is_directory(SP->BasePath) || SP->BasePath.empty()) {
1071 LLVM_DEBUG(
1072 dbgs() << "LibraryScanner::scanBaseDir: Invalid or empty basePath: "
1073 << SP->BasePath << "\n";);
1074 return;
1075 }
1076
1077 LLVM_DEBUG(dbgs() << "LibraryScanner::scanBaseDir: Scanning directory: "
1078 << SP->BasePath << "\n";);
1079 std::error_code EC;
1080
1081 SP->State.store(ScanState::Scanning);
1082
1083 for (sys::fs::directory_iterator It(SP->BasePath, EC), end; It != end && !EC;
1084 It.increment(EC)) {
1085 auto Entry = *It;
1086 if (!Entry.status())
1087 continue;
1088
1089 auto Status = *Entry.status();
1090 if (sys::fs::is_regular_file(Status) || sys::fs::is_symlink_file(Status)) {
1091 LLVM_DEBUG(dbgs() << " Found file: " << Entry.path() << "\n";);
1092
1093 std::string FinalPath;
1094 bool IsSymlink = sys::fs::is_symlink_file(Status);
1095
1096 // Resolve symlink
1097 if (IsSymlink) {
1098 LLVM_DEBUG(dbgs() << " Symlink → resolving...\n");
1099
1100 auto CanonicalOpt = ScanHelper.resolve(Entry.path(), EC);
1101 if (EC || !CanonicalOpt) {
1102 LLVM_DEBUG(dbgs() << " -> Skipped: resolve failed (EC="
1103 << EC.message() << ")\n");
1104 continue;
1105 }
1106
1107 FinalPath = std::move(*CanonicalOpt);
1108
1109 LLVM_DEBUG(dbgs() << " Canonical: " << FinalPath << "\n");
1110
1111 } else {
1112 // make absolute
1113 SmallString<256> Abs(Entry.path());
1115 FinalPath = Abs.str().str();
1116
1117 LLVM_DEBUG(dbgs() << " Regular: absolute = " << FinalPath << "\n");
1118 }
1119
1120 // Check if it's a directory — skip directories
1121 if (sys::fs::is_directory(Status)) {
1122 LLVM_DEBUG(dbgs() << " -> Skipped: path is a directory.\n";);
1123 continue;
1124 }
1125
1126 // async support ?
1127 handleLibrary(FinalPath, SP->Kind);
1128 }
1129 }
1130
1131 SP->State.store(ScanState::Scanned);
1132}
1133
1134void LibraryScanner::scanNext(PathType K, size_t BatchSize) {
1135 LLVM_DEBUG(dbgs() << "LibraryScanner::scanNext: Scanning next batch of size "
1136 << BatchSize << " for kind "
1137 << (K == PathType::User ? "User" : "System") << "\n";);
1138
1140 ScanHelper.getNextBatch(K, BatchSize, SearchPaths);
1141 for (const auto *SP : SearchPaths) {
1142 LLVM_DEBUG(dbgs() << " Scanning unit with basePath: " << SP->BasePath
1143 << "\n";);
1144 scanBaseDir(const_cast<LibrarySearchPath *>(SP));
1145 }
1146}
1147} // end namespace llvm::orc
std::deque< BasicBlock * > PathType
#define _
#define P(N)
static const char * name
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:114
#define PATH_MAX
Definition Utils.h:27
Base class for error info classes.
Definition Error.h:44
virtual std::string message() const
Return the error message as a string.
Definition Error.h:52
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
Error takeError()
Take ownership of the stored error.
Definition Error.h:612
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
void append(StringRef RHS)
Append from a StringRef.
Definition SmallString.h:68
StringRef str() const
Explicit conversion to StringRef.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
reference emplace_back(ArgTypes &&... Args)
void swap(SmallVectorImpl &RHS)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:712
static constexpr size_t npos
Definition StringRef.h:57
std::string str() const
str - Get the contents as an std::string.
Definition StringRef.h:225
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:261
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition StringRef.cpp:41
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition StringRef.h:611
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:47
ObjectFormatType getObjectFormat() const
Get the object format for this triple.
Definition Triple.h:440
ArchType getArch() const
Get the parsed architecture type of this triple.
Definition Triple.h:414
uint16_t getCharacteristics() const
Definition COFF.h:974
const Elf_Ehdr & getHeader() const
Definition ELF.h:325
Expected< Elf_Phdr_Range > program_headers() const
Iterate over program header table.
Definition ELF.h:422
Expected< StringRef > getStringTableForSymtab(const Elf_Shdr &Section) const
Definition ELF.h:1361
Expected< Elf_Dyn_Range > dynamicEntries() const
Definition ELF.cpp:625
Expected< Elf_Shdr_Range > sections() const
Definition ELF.h:1002
Expected< const uint8_t * > toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler=&defaultWarningHandler) const
Definition ELF.cpp:677
This class is the base class for all object file types.
Definition ObjectFile.h:231
static Expected< OwningBinary< ObjectFile > > createObjectFile(StringRef ObjectPath)
Validates and normalizes dynamic library paths.
bool isSharedLibrary(StringRef Path) const
std::optional< std::string > validate(StringRef Path) const
Validate the given path as a shared library.
std::optional< std::string > resolve(StringRef Stem, bool VariateLibStem=false) const
Performs placeholder substitution in dynamic library paths.
std::string substitute(StringRef input) const
void configure(StringRef loaderPath)
bool isTrackedBasePath(StringRef P) const
void getNextBatch(PathType Kind, size_t batchSize, SmallVectorImpl< const LibrarySearchPath * > &Out)
bool leftToScan(PathType K) const
void addBasePath(const std::string &P, PathType Kind=PathType::Unknown)
void scanNext(PathType Kind, size_t batchSize=1)
std::optional< ObjectFileLoader > take(StringRef Path)
Loads an object file and provides access to it.
static bool isArchitectureCompatible(const object::ObjectFile &Obj)
Expected< object::ObjectFile & > getObjectFile()
Get the loaded object file, or return an error if loading failed.
std::optional< std::string > readlinkCached(StringRef Path)
mode_t lstatCached(StringRef Path)
std::optional< std::string > realpathCached(StringRef Path, std::error_code &ec, StringRef base="", bool baseIsResolved=false, long symloopLevel=40)
std::optional< std::string > resolve(StringRef libStem, const DylibSubstitutor &Subst, DylibPathValidator &Validator) const
@ Entry
Definition COFF.h:862
@ IMAGE_FILE_DLL
The image file is a DLL.
Definition COFF.h:170
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ DF_1_PIE
Definition ELF.h:1677
@ SHT_DYNSYM
Definition ELF.h:1157
@ PT_INTERP
Definition ELF.h:1560
@ ET_DYN
Definition ELF.h:121
@ MH_DYLIB
Definition MachO.h:48
LLVM_ABI Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition Binary.cpp:45
Expected< LibraryDepsInfo > parseELF(const object::ELFFile< ELFT > &Elf)
LibraryScanner::LibraryDepsInfo LibraryDepsInfo
Expected< LibraryDepsInfo > parseMachODeps(const object::MachOObjectFile &Obj)
void createComponent(StringRef Path, StringRef BasePath, bool BaseIsResolved, SmallVector< StringRef, 16 > &Component)
void handleError(Error Err, StringRef context="")
void normalizePathSegments(SmallVector< StringRef, 16 > &PathParts)
bool isELFSharedLibrary(const object::ELFFile< ELFT > &ELFObj)
@ Resolved
Queried, materialization begun.
Definition Core.h:793
Expected< LibraryDepsInfo > parseDependencies(StringRef FilePath, object::ObjectFile *Obj)
bool isSharedLibraryObject(object::ObjectFile &Obj)
Expected< LibraryDepsInfo > parseELFDeps(const object::ELFObjectFileBase &Obj)
static Expected< StringRef > getDynamicStrTab(const object::ELFFile< ELFT > &Elf)
LLVM_ABI bool is_regular_file(const basic_file_status &status)
Does status represent a regular file?
Definition Path.cpp:1119
LLVM_ABI bool is_symlink_file(const basic_file_status &status)
Does status represent a symlink file?
Definition Path.cpp:1133
LLVM_ABI std::error_code real_path(const Twine &path, SmallVectorImpl< char > &output, bool expand_tilde=false)
Collapse all .
LLVM_ABI std::string getMainExecutable(const char *argv0, void *MainExecAddr)
Return the path to the main executable, given the value of argv[0] from program startup and the addre...
LLVM_ABI file_type get_file_type(const Twine &Path, bool Follow=true)
Does status represent a directory?
Definition Path.cpp:1098
LLVM_ABI std::error_code make_absolute(SmallVectorImpl< char > &path)
Make path an absolute path.
Definition Path.cpp:962
LLVM_ABI std::error_code current_path(SmallVectorImpl< char > &result)
Get the current path.
LLVM_ABI bool is_directory(const basic_file_status &status)
Does status represent a directory?
Definition Path.cpp:1105
LLVM_ABI StringRef get_separator(Style style=Style::native)
Return the preferred separator for this platform.
Definition Path.cpp:610
LLVM_ABI void remove_filename(SmallVectorImpl< char > &path, Style style=Style::native)
Remove the last component from path unless it is the root dir.
Definition Path.cpp:475
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:468
LLVM_ABI bool is_relative(const Twine &path, Style style=Style::native)
Is path relative?
Definition Path.cpp:700
LLVM_ABI StringRef filename(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get filename.
Definition Path.cpp:578
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:672
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:457
LLVM_ABI bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
LLVM_ABI bool is_separator(char value, Style style=Style::native)
Check whether the given char is a path separator on the host OS.
Definition Path.cpp:602
const char EnvPathSeparator
This is the OS-specific separator for PATH like environment variables:
Definition Program.h:33
LLVM_ABI std::string getProcessTriple()
getProcessTriple() - Return an appropriate target triple for generating code to be loaded into the cu...
Definition Host.cpp:2559
LLVM_ABI file_magic identify_magic(StringRef magic)
Identify the type of a binary file based on how magical it is.
Definition Magic.cpp:33
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition Error.cpp:98
Error handleErrors(Error E, HandlerTs &&... Hs)
Pass the ErrorInfo(s) contained in E to their respective handlers.
Definition Error.h:967
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1305
LLVM_ABI void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")
SplitString - Split up the specified string according to the specified delimiters,...
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
Error make_error(ArgTs &&... Args)
Make a Error instance representing failure using the given error info type.
Definition Error.h:340
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:189
void consumeError(Error Err)
Consume a Error without doing anything.
Definition Error.h:1083
uint32_t name
Definition MachO.h:614
file_magic - An "enum class" enumeration of file types based on magic (the first N bytes of the file)...
Definition Magic.h:21
@ archive
ar style archive file
Definition Magic.h:26
@ elf_shared_object
ELF dynamically linked shared lib.
Definition Magic.h:30
@ macho_dynamically_linked_shared_lib
Mach-O dynlinked shared lib.
Definition Magic.h:38
@ macho_dynamically_linked_shared_lib_stub
Mach-O Shared lib stub.
Definition Magic.h:41
@ pecoff_executable
PECOFF executable file.
Definition Magic.h:50
@ macho_universal_binary
Mach-O universal binary.
Definition Magic.h:44
@ macho_fixed_virtual_memory_shared_lib
Mach-O Shared Lib, FVM.
Definition Magic.h:35