Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -138,6 +138,7 @@ void readSectionPatterns(StringRef OutSec, bool Keep); size_t getPos(); + std::pair getLineColumn(); std::vector parseHex(StringRef S); StringSaver Saver; @@ -172,11 +173,31 @@ } } +static StringRef getLine(StringRef S, size_t Pos) { + size_t Begin = S.rfind('\n', Pos); + size_t End = S.find('\n', Pos); + if (Begin == StringRef::npos) + Begin = 0; + if (End == StringRef::npos) + End = S.size(); + return S.substr(Begin, End - Begin); +} + +std::pair ScriptParser::getLineColumn() { + StringRef Tok = Tokens[Pos == 0 ? 0 : Pos - 1]; + StringRef Line = getLine(Input, Tok.data() - Input.data()); + size_t Col = Tok.data() - Line.data() + Tok.size() / 2; + return {Line, Col}; +} + // We don't want to record cascading errors. Keep only the first one. void ScriptParser::setError(const Twine &Msg) { if (Error) return; error("line " + Twine(getPos()) + ": " + Msg); + std::pair LineCol = getLineColumn(); + error(LineCol.first); + error(std::string(LineCol.second, ' ') + "^"); Error = true; } Index: test/ELF/linkerscript-diagnostic.s =================================================================== --- test/ELF/linkerscript-diagnostic.s +++ test/ELF/linkerscript-diagnostic.s @@ -42,3 +42,25 @@ # RUN: echo ".temp + { *(.temp) } }" >> %t.script # RUN: not ld.lld -shared %t -o %t1 --script %t.script 2>&1 | FileCheck -check-prefix=ERR5 %s # ERR5: line 6: + +## Check that text of lines and pointer to 'bad' token are working ok. +# RUN: echo "UNKNOWN_TAG {" > %t.script +# RUN: echo ".text : { *(.text) }" >> %t.script +# RUN: echo ".keep : { *(.keep) }" >> %t.script +# RUN: echo ".temp : { *(.temp) } }" >> %t.script +# RUN: not ld.lld -shared %t -o %t1 --script %t.script > %t.log 2>&1 +# FileCheck -check-prefix=ERR6 %s < %t.log +# ERR6: line 1: +# ERR6: UNKNOWN_TAG { +# ERR6: {{[ ]{5}\^}} + +## One more check that text of lines and pointer to 'bad' token are working ok. +# RUN: echo "SECTIONS {" > %t.script +# RUN: echo ".text : { *(.text) }" >> %t.script +# RUN: echo ".keep : { *(.keep) }" >> %t.script +# RUN: echo "boom .temp : { *(.temp) } }" >> %t.script +# RUN: not ld.lld -shared %t -o %t1 --script %t.script > %t.log 2>&1 +# FileCheck -check-prefix=ERR7 %s < %t.log +# ERR7: line 4: +# ERR7: boom .temp : { *(.temp) } +# ERR7: {{[ ]{7}\^}}