dmd icon indicating copy to clipboard operation
dmd copied to clipboard

Demangle symbols in linker errors

Open dkorpel opened this issue 1 year ago • 4 comments

Linker errors are notoriously cryptic, and read almost like internal compiler errors. With this PR, when dmd invokes the linker and sees the output contains missing (mangled) symbols, it prints them in its own error format, with demangled symbols.

It also provides some suggestions (-i, -L, pragma(lib), -main) for new users who are not familiar with D's 'old-fashioned' compilation model.

I don't want to write an end-to-end test depending on various linkers to be installed, so I made a unittest with a mock ErrorSink, which is very portable.

Todo:

  • Add code to parse OSX linker errors
  • Pipe linker output on Windows as well (parsing sample output of LINK.exe works in the unittest)
  • Determine the best default output format. Currently it prints linker output followed by translated errors, but it could also print it after, interspersed, or not at all.

dkorpel avatar Jan 11 '24 22:01 dkorpel

Thanks for your pull request and interest in making D better, @dkorpel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon. Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
5573 enhancement Compiler (not linker) should generate an error for missing main()

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#16021"

dlang-bot avatar Jan 11 '24 22:01 dlang-bot

I appreciate the sentiment. A couple of notes:

  • the original error message tells me what linker is being used (if not by name (ld, lld), then by format of the error message (gold, MS).
  • could we also print what arguments were passed to the linker?
  • the unmangled symbol name is still useful to cross-reference with mn and other file utilities, it is not clear from the diff what the (entire) output of a missing symbol now is.

thewilsonator avatar Jan 11 '24 23:01 thewilsonator

  • the unmangled symbol name is still useful to cross-reference with mn and other file utilities, it is not clear from the diff what the (entire) output of a missing symbol now is.

nm -C dlang (or --demangle=dlang) has been available for nearly a decade now (actually probably more). You can do the same with ld --demangle=dlang.

The barrier for using these third party tools could be lowered however. If you want to go radical, give dmd positional arguments - dmd compile app.d, dmd link app.o, dmd dump asm app.exe, dmd dump symbols app.o, dmd unittest app.d, ... I digress.

ibuclaw avatar Jan 12 '24 09:01 ibuclaw

A couple of notes:

That's why currently the new errors don't replace the linker output, but are printed in addition to it.

could we also print what arguments were passed to the linker?

The linker command is printed with -v, but that prints a whole lot more. I think it would also be good to print the linker command in case of linker errors, I'll try that in a separate PR.

dkorpel avatar Jan 12 '24 09:01 dkorpel

Ready for review @RazvanN7

dkorpel avatar Apr 18 '24 15:04 dkorpel

@dkorpel Hi Dennis, the work is appreciated but I really don't like that the actual missing symbol (and referencing symbol) is no longer part of the output; do I understand the correctly? Going from mangled to demangled is very straightforward (ddemangle), while the opposite direction is very non trivial. To solve and investigate linking issues, you really need to know the actual - i.e mangled - symbol(s). Please add that back in the output!

JohanEngelen avatar Jun 02 '24 20:06 JohanEngelen

A couple of notes:

That's why currently the new errors don't replace the linker output, but are printed in addition to it.

Great, thanks! Sorry for the noise.

JohanEngelen avatar Jun 02 '24 20:06 JohanEngelen