This is an archive of the discontinued LLVM Phabricator instance.

[lld-macho] Print the name of functions containing undefined references
ClosedPublic

Authored by BertalanD on Jun 13 2022, 2:00 PM.

Details

Summary

The error used to look like this:

ld64.lld: error: undefined symbol: _foo
>>> referenced by /path/to/bar.o

Now it displays the name of the function that contains the undefined
reference as well:

ld64.lld: error: undefined symbol: _foo
>>> referenced by /path/to/bar.o:(symbol _baz+0x4)

I plan to use debug information to provide even more context in a future
commit.

Please commit this diff with the following author info:
Daniel Bertalan <dani@danielbertalan.dev>

Diff Detail

Event Timeline

BertalanD created this revision.Jun 13 2022, 2:00 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 13 2022, 2:00 PM
BertalanD requested review of this revision.Jun 13 2022, 2:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 13 2022, 2:00 PM
BertalanD edited the summary of this revision. (Show Details)Jun 13 2022, 2:02 PM
thakis accepted this revision.Jun 13 2022, 4:01 PM

This looks excellent.

The ELF and COFF ports as well as ld64 don't print the offset:

% out/gn/bin/clang -fuse-ld=lld test.cc -isysroot $(xcrun -show-sdk-path)  --target=x86_64-pc-linux -nostdlib
ld.lld: error: undefined symbol: f()
>>> referenced by test.cc
>>>               /var/folders/w6/wpbtszrs7jl9dc9l5qtdkvg00000gn/T/test-d7614d.o:(main)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
% out/gn/bin/clang-cl -fuse-ld=lld test.cc  /link /nodefaultlib                                     
lld-link: error: <root>: undefined symbol: mainCRTStartup
lld-link: error: undefined symbol: void __cdecl f(void)
>>> referenced by /var/folders/w6/wpbtszrs7jl9dc9l5qtdkvg00000gn/T/test-611b03.obj:(main)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
% out/gn/bin/clang test.o -isysroot $(xcrun -show-sdk-path)              
Undefined symbols for architecture arm64:
  "f()", referenced from:
      _main in test.o
ld: symbol(s) not found for architecture arm64

Should we omit it too, at least for undefined symbol diags?

This revision is now accepted and ready to land.Jun 13 2022, 4:01 PM
int3 accepted this revision.Jun 14 2022, 2:56 AM
int3 added a subscriber: int3.

Should we omit it too, at least for undefined symbol diags?

IMO we are strictly providing more useful info here so I think it makes more sense for us to keep it this way and maybe have the other ports changed to match at some point

Should we omit it too, at least for undefined symbol diags?

IMO we are strictly providing more useful info here so I think it makes more sense for us to keep it this way and maybe have the other ports changed to match at some point

What's useful about an offset about a function?

I think for almost everyone, the offset is at best noise they'll ignore or at worst something that confuses them.

And even for people who know what it means and can read assembly, what's the added value of knowing which offset in a function has the reference?

Let's land this as-is for now. I'm sure once Jez reads my reply he will be swayed by my flawless reasoning, but implementing this suggestion will require more code on top of the code that is already here, so might as well do that suggestion in a follow-up and keep this one smaller. (And everyone agrees that this patch as-is is a big step forward already.)

int3 added a comment.Jun 14 2022, 6:40 AM

And even for people who know what it means and can read assembly, what's the added value of knowing which offset in a function has the reference?

Okay fair. I guess if we printed out the actual debug line number, that might be more useful (since the user might want to delete that reference in the source.)

It appears that LLD-ELF only emits the address offset when reporting on an invalid relocation, which seems like a reasonable approach to follow, since in that case we actually do need the address to diagnose the error

int3 added a comment.Jun 14 2022, 6:41 AM

One minute apart :) but yeah we can address that in a followup