@@ -85,16 +85,42 @@ bool Input::isPathValid(const std::string &Path) const {
8585 return true ;
8686}
8787
88+ std::string Input::expandSysrootMarkers (llvm::StringRef Name,
89+ const SearchDirs &PSearchDirs,
90+ DiagnosticEngine &DiagEngine) {
91+ llvm::StringRef Suffix;
92+ if (Name.starts_with (" =" ))
93+ Suffix = Name.substr (1 );
94+ else if (Name.starts_with (" $SYSROOT" ))
95+ Suffix = Name.substr (strlen (" $SYSROOT" ));
96+ else
97+ return Name.str ();
98+
99+ std::string ExpandedPath;
100+ if (PSearchDirs.hasSysRoot ())
101+ ExpandedPath = (PSearchDirs.sysroot ().native () + Suffix).str ();
102+ else
103+ ExpandedPath = Suffix.str ();
104+
105+ DiagEngine.raise (Diag::verbose_sysroot_expansion) << Name << ExpandedPath;
106+ return ExpandedPath;
107+ }
108+
88109// / \return True if path able to be resolved, otherwise false
89110bool Input::resolvePath (const LinkerConfig &PConfig) {
90111 if (ResolvedPath)
91112 return true ;
92113 if (PConfig.options ().hasMappingFile () && !isInternal ())
93114 return resolvePathMappingFile (PConfig);
94115 auto &PSearchDirs = PConfig.directories ();
116+
117+ std::string ExpandedFileName = FileName;
118+ if (Type == Input::InputType::Script || Type == Input::InputType::Default)
119+ ExpandedFileName = expandSysrootMarkers (FileName, PSearchDirs, *DiagEngine);
120+
95121 switch (Type) {
96122 default :
97- ResolvedPath = eld::sys::fs::Path (FileName );
123+ ResolvedPath = eld::sys::fs::Path (ExpandedFileName );
98124 break ;
99125 case Input::Internal:
100126 ResolvedPath = eld::sys::fs::Path (FileName);
@@ -103,10 +129,11 @@ bool Input::resolvePath(const LinkerConfig &PConfig) {
103129 if (Type == Input::Script) {
104130 if (shouldPrependSysrootToScriptInput (PConfig)) {
105131 ResolvedPath = PSearchDirs.sysroot ();
106- ResolvedPath->append (FileName );
132+ ResolvedPath->append (ExpandedFileName );
107133 }
108134 if (!llvm::sys::fs::exists (ResolvedPath->native ())) {
109- const sys::fs::Path *P = PSearchDirs.find (FileName, Input::Script);
135+ const sys::fs::Path *P =
136+ PSearchDirs.find (ExpandedFileName, Input::InputType::Script);
110137 if (P != nullptr )
111138 ResolvedPath = *P;
112139 }
0 commit comments