Index: Common/ErrorHandler.cpp =================================================================== --- Common/ErrorHandler.cpp +++ Common/ErrorHandler.cpp @@ -16,6 +16,7 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/raw_ostream.h" #include +#include #if !defined(_MSC_VER) && !defined(__MINGW32__) #include @@ -84,8 +85,36 @@ [&](ErrorInfoBase &EIB) { error(EIB.message()); }); } -void ErrorHandler::print(StringRef S, raw_ostream::Colors C) { - *ErrorOS << LogName << ": "; +static std::string getLocation(std::string Msg, std::string Default) { + static std::vector Regexes{ + std::regex( + R"(^can't create dynamic relocation .* against (?:local |)symbol .*\n>>> defined in .*\n>>> referenced by (\S+):(\d+))"), + std::regex(R"(^duplicate symbol:.*\n>>> defined at (\S+):(\d+))"), + std::regex( + R"(^undefined symbol:.*\n>>> referenced by (\S+):(\d+)\n.*)"), + std::regex(R"(^undefined symbol:.*\n>>> referenced by (.*):)")}; + + std::smatch Match; + for (std::regex &Re : Regexes) + if (std::regex_search(Msg, Match, Re)) { + return Match.size() > 2 ? Match.str(1) + "(" + Match.str(2) + ")" + : Match.str(1); + } + + return Default; +} + +void ErrorHandler::printHeader(StringRef S, raw_ostream::Colors C, + const Twine &Msg) { + + if (VsDiag) { + // Visual Studio-style error message starts with an error location. + // If a location cannot be extracted then we default to LogName + *ErrorOS << getLocation(Msg.str(), LogName) << ": "; + } else { + *ErrorOS << LogName << ": "; + } + if (ColorDiagnostics) { ErrorOS->changeColor(C, true); *ErrorOS << S; @@ -116,7 +145,7 @@ std::lock_guard Lock(Mu); newline(ErrorOS, Msg); - print("warning: ", raw_ostream::MAGENTA); + printHeader("warning: ", raw_ostream::MAGENTA, Msg); *ErrorOS << Msg << "\n"; } @@ -125,10 +154,10 @@ newline(ErrorOS, Msg); if (ErrorLimit == 0 || ErrorCount < ErrorLimit) { - print("error: ", raw_ostream::RED); + printHeader("error: ", raw_ostream::RED, Msg); *ErrorOS << Msg << "\n"; } else if (ErrorCount == ErrorLimit) { - print("error: ", raw_ostream::RED); + printHeader("error: ", raw_ostream::RED, Msg); *ErrorOS << ErrorLimitExceededMsg << "\n"; if (ExitEarly) exitLld(1); Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -758,6 +758,7 @@ errorHandler().Verbose = Args.hasArg(OPT_verbose); errorHandler().FatalWarnings = Args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); + errorHandler().VsDiag = Args.hasArg(OPT_vs_diagnostics); ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true); Config->AllowMultipleDefinition = Index: ELF/Options.td =================================================================== --- ELF/Options.td +++ ELF/Options.td @@ -359,6 +359,9 @@ def version: F<"version">, HelpText<"Display the version number and exit">; +def vs_diagnostics: F<"vs-diagnostics">, +HelpText<"Changes diagnostic output format to better match Microsoft Visual Studio">; + defm version_script: Eq<"version-script", "Read a version script">; defm warn_backrefs: B<"warn-backrefs", Index: include/lld/Common/ErrorHandler.h =================================================================== --- include/lld/Common/ErrorHandler.h +++ include/lld/Common/ErrorHandler.h @@ -91,6 +91,7 @@ bool ExitEarly = true; bool FatalWarnings = false; bool Verbose = false; + bool VsDiag = false; void error(const Twine &Msg); LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg); @@ -101,7 +102,7 @@ std::unique_ptr OutputBuffer; private: - void print(StringRef S, raw_ostream::Colors C); + void printHeader(StringRef S, raw_ostream::Colors C, const Twine &Msg); }; /// Returns the default error handler. Index: test/ELF/Inputs/vs-diag-duplicate2.s =================================================================== --- /dev/null +++ test/ELF/Inputs/vs-diag-duplicate2.s @@ -0,0 +1,31 @@ +.global foo, bar + +.text +foo: + nop + +.file 1 "duplicate2.s" +.loc 1 20 +bar: + nop + +.section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + +.section .debug_info,"",@progbits + .long .Lend0 - .Lbegin0 # Length of Unit +.Lbegin0: + .short 4 # DWARF version number + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 8 # Address Size (in bytes) + .byte 1 # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit + .long .debug_line # DW_AT_stmt_list +.Lend0: + .section .debug_line,"",@progbits Index: test/ELF/Inputs/vs-diag-duplicate3.s =================================================================== --- /dev/null +++ test/ELF/Inputs/vs-diag-duplicate3.s @@ -0,0 +1,6 @@ +.file "duplicate3.s" + +.global baz +.text +baz: + nop Index: test/ELF/vs-diag-duplicate.s =================================================================== --- /dev/null +++ test/ELF/vs-diag-duplicate.s @@ -0,0 +1,63 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/vs-diag-duplicate2.s -o %t2.o +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %S/Inputs/vs-diag-duplicate3.s -o %t3.o +// RUN: not ld.lld --vs-diagnostics %t1.o %t2.o %t3.o -o %tout 2>&1 | FileCheck %s + +// Case 1. Both symbols have full source location. +// CHECK: duplicate.s(15): error: duplicate symbol: bar +// CHECK-NEXT: >>> defined at duplicate.s:15 +// CHECK-NEXT: >>>{{.*}}1.o:(.text+0x{{.+}}) +// CHECK: >>> defined at duplicate2.s:20 +// CHECK: >>>{{.*}}2.o:(.text+0x{{.+}}) + +// Case 2. The source locations are unknown for both symbols. +// CHECK: {{.*}}ld.lld{{.*}}: error: duplicate symbol: foo +// CHECK-NEXT: >>> defined at {{.*}}1.o:(.text+0x{{.+}}) +// CHECK-NEXT: >>> defined at {{.*}}2.o:(.text+0x{{.+}}) + +// Case 3. For the second definition of `baz` we know only the source file found in a STT_FILE symbol. +// CHECK: duplicate.s(30): error: duplicate symbol: baz +// CHECK-NEXT: >>> defined at duplicate.s:30 +// CHECK-NEXT: >>> {{.*}}1.o:(.text+0x{{.+}}) +// CHECK-NEXT: >>> defined at duplicate3.s +// CHECK-NEXT: >>> {{.*}}3.o:(.text+0x{{.+}}) + +.global _start, foo, bar, baz +.text +_start: + nop + +foo: + nop + +.file 1 "duplicate.s" +.loc 1 15 + +bar: + nop + +.loc 1 30 +baz: + nop + +.section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + +.section .debug_info,"",@progbits + .long .Lend0 - .Lbegin0 # Length of Unit +.Lbegin0: + .short 4 # DWARF version number + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 8 # Address Size (in bytes) + .byte 1 # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit + .long .debug_line # DW_AT_stmt_list +.Lend0: + .section .debug_line,"",@progbits Index: test/ELF/vs-diag-dyn.s =================================================================== --- /dev/null +++ test/ELF/vs-diag-dyn.s @@ -0,0 +1,35 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o +// RUN: not ld.lld -shared --vs-diagnostics %t.o -o /dev/null 2>&1 | FileCheck %s + +// CHECK: dyn.s(15): error: can't create dynamic relocation R_X86_64_64 against local symbol in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output +// CHECK-NEXT: >>> defined in {{.*}}.o +// CHECK-NEXT: >>> referenced by dyn.s:15 +// CHECK-NEXT: >>>{{.*}}.o:(.text+0x{{.+}}) + +.file 1 "dyn.s" +.loc 1 15 + +foo: +.quad foo + +.section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + +.section .debug_info,"",@progbits + .long .Lend0 - .Lbegin0 # Length of Unit +.Lbegin0: + .short 4 # DWARF version number + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 8 # Address Size (in bytes) + .byte 1 # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit + .long .debug_line # DW_AT_stmt_list +.Lend0: + .section .debug_line,"",@progbits Index: test/ELF/vs-diag-undef1.s =================================================================== --- /dev/null +++ test/ELF/vs-diag-undef1.s @@ -0,0 +1,15 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o +// RUN: not ld.lld --vs-diagnostics %t1.o -o %tout 2>&1 \ +// RUN: | FileCheck -check-prefix=ERR -check-prefix=CHECK %s +// RUN: ld.lld --vs-diagnostics --warn-unresolved-symbols %t1.o -o %tout 2>&1 \ +// RUN: | FileCheck -check-prefix=WARN -check-prefix=CHECK %s + +// ERR: {{.*}}undef1.s{{.*}}: error: undefined symbol: foo +// WARN: {{.*}}undef1.s{{.*}}: warning: undefined symbol: foo +// CHECK-NEXT: >>> referenced by {{.*}}1.o:(.text+0x{{.+}}) + +.global _start, foo +.text +_start: + jmp foo \ No newline at end of file Index: test/ELF/vs-diag-undef2.s =================================================================== --- /dev/null +++ test/ELF/vs-diag-undef2.s @@ -0,0 +1,18 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o +// RUN: not ld.lld --vs-diagnostics %t1.o -o %tout 2>&1 \ +// RUN: | FileCheck -check-prefix=ERR -check-prefix=CHECK %s +// RUN: ld.lld --vs-diagnostics --warn-unresolved-symbols %t1.o -o %tout 2>&1 \ +// RUN: | FileCheck -check-prefix=WARN -check-prefix=CHECK %s + +// ERR: {{.*}}ld.lld{{.*}}: error: undefined symbol: foo +// WARN: {{.*}}ld.lld{{.*}}: warning: undefined symbol: foo +// CHECK-NEXT: >>> referenced by undef2.s +// CHECK-NEXT: >>> {{.*}}1.o:(.text+0x{{.+}}) + +.file "undef2.s" + +.global _start, foo +.text +_start: + jmp foo Index: test/ELF/vs-diag-undef3.s =================================================================== --- /dev/null +++ test/ELF/vs-diag-undef3.s @@ -0,0 +1,40 @@ +// REQUIRES: x86 +// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t1.o +// RUN: not ld.lld --vs-diagnostics %t1.o -o %tout 2>&1 \ +// RUN: | FileCheck -check-prefix=ERR -check-prefix=CHECK %s +// RUN: ld.lld --vs-diagnostics --warn-unresolved-symbols %t1.o -o %tout 2>&1 \ +// RUN: | FileCheck -check-prefix=WARN -check-prefix=CHECK %s + +// ERR: undef3.s(15): error: undefined symbol: foo +// WARN: undef3.s(15): warning: undefined symbol: foo +// CHECK: >>> referenced by undef3.s:15 +// CHECK-NEXT: >>> {{.*}}1.o:(.text+0x{{.+}}) + +.file 1 "undef3.s" + +.global _start, foo +.text +_start: +.loc 1 15 + jmp foo + +.section .debug_abbrev,"",@progbits + .byte 1 # Abbreviation Code + .byte 17 # DW_TAG_compile_unit + .byte 0 # DW_CHILDREN_no + .byte 16 # DW_AT_stmt_list + .byte 23 # DW_FORM_sec_offset + .byte 0 # EOM(1) + .byte 0 # EOM(2) + .byte 0 # EOM(3) + +.section .debug_info,"",@progbits + .long .Lend0 - .Lbegin0 # Length of Unit +.Lbegin0: + .short 4 # DWARF version number + .long .debug_abbrev # Offset Into Abbrev. Section + .byte 8 # Address Size (in bytes) + .byte 1 # Abbrev [1] 0xb:0x1f DW_TAG_compile_unit + .long .debug_line # DW_AT_stmt_list +.Lend0: + .section .debug_line,"",@progbits