llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Linker error when using an archive created with `ar`

Open PeixinQiao opened this issue 4 years ago • 19 comments

There seems to be one problem with the current driver:

$ cat case.f90 
program m
end
$ flang-new case.f90 -c
$ ar -r a.a case.o
$ flang-new a.a -o a.out
/usr/bin/ld: /.../build/bin/../lib/libFortran_main.a(Fortran_main.c.o): in function `main':
/.../f18-llvm-project/flang/runtime/Fortran_main.c:11: undefined reference to `_QQmain'
flang-new: error: linker command failed with exit code 1 (use -v to see invocation)
$ ld --version
GNU ld version 2.31
$ ld.lld ... a.a -lFortran_main ... ! works ok
$ ld ... -lFortran_main a.a ... ! works ok
$ ld ... a.a -lFortran_main ...
ld: /.../build/bin/../lib/libFortran_main.a(Fortran_main.c.o): in function `main':
/.../f18-llvm-project/flang/runtime/Fortran_main.c:11: undefined reference to `_QQmain'

flang-new put _QQmain in binary file compiled by program and put main in libFortran_main.a . gfortran put both MAIN_ and main in binary file compiled by program , so it does not have this issue.

PeixinQiao avatar Apr 07 '22 00:04 PeixinQiao

@llvm/issue-subscribers-flang-driver

llvmbot avatar Apr 07 '22 00:04 llvmbot

Thanks for posting this @PeixinQiao !

I confirm that I was able to reproduce this. Just to clarify - currently this can only be tested with the fir-dev branch. Once https://reviews.llvm.org/D122008 is merged, one will be able to reproduce this using the main branch.

Note that this is a very specific usage. In particular, one could use the following two approaches instead (that do work fine for me):

$ flang-new case.f90

or

$ flang-new case.f90 -c
$ flang-new case.o

What's unique in your case is the usage of ar ($ ar -r a.a case.o). I don't understand what's special about using ar here. In particular, why are you getting undefined reference to '_QQmain'? I'd need to dig a bit deeper.

EDIT I've made the title a bit more specific.

-Andrzej

banach-space avatar Apr 21 '22 13:04 banach-space

@llvm/issue-subscribers-good-first-issue

llvmbot avatar Apr 21 '22 13:04 llvmbot

Here is my understanding of the situation (simplified).

Consider the following object files and archives (static libraries) with exported (T) and imported (U) symbols (notation from gnu nm):

prog.o        T: _QQmain
              U: RT

prog.a        (archive containing prog.o, same symbols)

libmain.a     T: main
              U: _QQmain, RT

libruntime.a  T: RT
              U: -

The linker keeps a symbol table of T and U. The link line is processed from left to right.

  • When an object file is encountered, its symbols are added to the symbol table. If a symbol which is exported by the object file was previously in U, it is moved to T.
  • When an archive is encountered each contained object file is processed as above, but only if the object file exports a symbol which was previously in U.

Now, $ flang prog.o produces the linker invocation $ ld prog.o -lmain -lruntime

As each object is processed, the symbol table evolves as follows.

Starting point  T: -
                U: main

prog.o          T: _QQmain
                U: main, RT

libmain.a       T: _QQmain, main
                U: RT

libruntime.a    T: _QQmain, main, RT
                U: -

All symbols are resolved, and linking succeeds. On the other hand,

$ flang prog.a produces the linker invocation $ ld prog.a -lmain -lruntime

with symbol table evolution

Starting point  T: -
                U: main

prog.a          T: -
                U: main
		(this archive is discarded, as it does not export any unresolved symbol)

libmain.a       T: main
                U: _QQmain, RT

libruntime.a    T: main, RT
                U: _QQmain

The symbol _QQmain remains unresolved, and linking fails.

I see three possible ways of fixing this: a) Put -lFortran_main before the user code in the link line. Then _QQmain will be in U when the archive exporting it is processed. b) Add "--undefined _QQmain" to the link line. c) Remove Fortran_main.a and paste its content (the main function) into (a suitable intermediate form of) any translation unit which exports _QQmain

As @PeixinQiao mentioned, c) appears to be how gfortran does it. I am leaning towards b) -- it should almost be a one-liner, we just need some conditions so that we do not leave _QQmain as an undefined symbol when building shared objects (and there might be other cases I have overlooked). I suppose there are similar flags for other linkers we should support. I do not like a) particularly well, it appears a bit hacky.

What are your thoughts? Have I missed something?

ekieri avatar Aug 24 '22 18:08 ekieri

a) Put -lFortran_main before the user code in the link line. Then _QQmain will be in U when the archive exporting it is processed.

This seems to fail if the main function is in C code because of duplicated main

b) Add "--undefined _QQmain" to the link line.

What if the _QQmain is really missed for some reason?

c) appears to be how gfortran does it.

Agree. Not sure if this is the right solution.

PeixinQiao avatar Aug 25 '22 01:08 PeixinQiao

@banach-space What do you think? Are you still working on this?

PeixinQiao avatar Aug 25 '22 01:08 PeixinQiao

Thanks for the detailed analysis and breakdown, @ekieri! One thing I still don't understand - why doesn't "prog.a" export __QQMain - "prog.o" does, doesn't it?

a) Put -lFortran_main before the user code in the link line. Then _QQmain will be in U when the archive exporting it is processed.

This seems to fail if the main function is in C code because of duplicated main

If main is defined in two different places then shouldn't such compilation just fail? Also note that we haven't really tried mixed-source projects yet (e.g. C and Fortran). In cases where flang-new is used to generate an executable, it seems reasonable to me that the definition of main should come from Fortran/Flang.

b) Add "--undefined _QQmain" to the link line.

What if the _QQmain is really missed for some reason?

With and without "--undefined _QQmain", users will see a linker error if __QQmain is missing (and they are trying to generate an executable).

c) appears to be how gfortran does it.

Agree. Not sure if this is the right solution.

This would be the most intrusive approach requiring re-designing Fortran_main. I didn't participate in the process of designing/creating it (I just copied it across from "f18-llvm-project/fir-dev" to "llvm-project/main"), so no strong opinion here.

To me both a) and b) would be OK. b) is more explicit, i.e. people investigating the linker invocation will see that Flang requires this symbol to be available/defined. But then it might be confusing to users (i.e. "why is this needed?"). Relying on the order of libraries is less explicit, but the linker invocation stays cleaner.

Am I missing or misunderstanding something here?

Are you still working on this?

Sadly not, I've been distracted with other things.

-Andrzej

banach-space avatar Aug 25 '22 15:08 banach-space

One thing I still don't understand - why doesn't "prog.a" export __QQMain - "prog.o" does, doesn't it?

prog.a does export _QQmain, but it does not help. An (object file inside an) archive is only included in the linking if one of its exported symbol is marked as unresolved in the symbol table, and in this case it is not yet. It is imported by libmain.a, which is not yet processed (as it stands to the right of prog.a in the link line).

a) Put -lFortran_main before the user code in the link line. Then _QQmain will be in U when the archive exporting it is processed.

This seems to fail if the main function is in C code because of duplicated main

This makes sense, and IMHO it disqualifies option a).

If main is defined in two different places then shouldn't such compilation just fail? Also note that we haven't really tried mixed-source projects yet (e.g. C and Fortran). In cases where flang-new is used to generate an executable, it seems reasonable to me that the definition of main should come from Fortran/Flang.

Note that main is not defined in two different places by the user. libFortran_main.a is added to the link line by the driver, most users will be unaware of it.

In my understanding, mixed c/fortran projects are quite common. Even though it is not the highest priority right now, I think we should facilitate it when we have the choice. I would suppose it is always easier to link mixed projects with flang-new than with clang (as the former automatically adds the fortran runtime to the link line), so I think flang-new should handle also the cases where main is defined in c.

b) Add "--undefined _QQmain" to the link line.

What if the _QQmain is really missed for some reason?

With and without "--undefined _QQmain", users will see a linker error if __QQmain is missing (and they are trying to generate an executable).

When "--undefined SYMBOL" is passed, linking actually succeeds even when SYMBOL is missing. SYMBOL will then be an undefined symbol in the linked object, but will not make much harm as long as it is not referenced. In contrast, "--require-defined SYMBOL" also adds SYMBOL as unresolved in the symbol table, but fails the link job if it is not resolved. So, under option b), a few situations can arise:

  • No main, fortran nor c, is provided. libFortran_main.a (which exports main and imports _QQmain) is linked in, and linking fails as _QQmain is not resolved.
  • A fortran main program is defined in an object file or archive. It will define _QQmain, which will be called by main of libFortran_main.a, and be the entry point of the program.
  • A c main function is defined in an object file or archive. It will be the entry point of the program, libFortran_main.a will not be linked in as main is already defined. _QQmain will remain an unresolved symbol in the executable, but linking will succeed.
  • We are building an so/dll/dylib, with no main in the user code. libFortran_main.a will not be linked in since main is not expected in a shared object. _QQmain will remain undefined in the so, but linking will succeed. Given option b), I would suggest flang-new does not pass "--undefined _QQmain" in this situation.
  • Both a c and a fortran main are defined in user code. main will already be defined, so libFortran_main.a is not linked in. _QQmain will be resolved by the fortran main program, but will not be called from anywhere as libFortran_main.a is missing. The c main will be the entry point of the executable (which will link successfully). This situation is a bit unfortunate, I would rather see linking failing here.

All of this is in a linux context. I do not know what will happen on windows, but I would bet it will be different ...

c) appears to be how gfortran does it.

Agree. Not sure if this is the right solution.

This would be the most intrusive approach requiring re-designing Fortran_main. I didn't participate in the process of designing/creating it (I just copied it across from "f18-llvm-project/fir-dev" to "llvm-project/main"), so no strong opinion here.

Yes, this would be quite intrusive, would require a fair bit of work and a wider discussion. But I think it would solve all the problems and imperfections with the other options that we have come up with so far.

Emil

ekieri avatar Aug 25 '22 19:08 ekieri

One thing I still don't understand - why doesn't "prog.a" export __QQMain - "prog.o" does, doesn't it?

prog.a does export _QQmain, but it does not help. An (object file inside an) archive is only included in the linking if one of its exported symbol is marked as unresolved in the symbol table, and in this case it is not yet. It is imported by libmain.a, which is not yet processed (as it stands to the right of prog.a in the link line).

Sorry, you did mention this in your earlier post and I missed it, my bad! For reference, here's relevant doc from Oracle.

If main is defined in two different places then shouldn't such compilation just fail?

Note that main is not defined in two different places by the user. libFortran_main.a is added to the link line by the driver, most users will be unaware of it.

Fair point. I think that documenting "libFortran_main.a" and how it works would really help. In general, it would be good to help Flang users understand the internal workflow (this affects them in a very important way).

In my understanding, mixed c/fortran projects are quite common. Even though it is not the highest priority right now, I think we should facilitate it when we have the choice. I would suppose it is always easier to link mixed projects with flang-new than with clang (as the former automatically adds the fortran runtime to the link line), so I think flang-new should handle also the cases where main is defined in c.

These are very good points. We should definitely keep mixed C/Fortran projects in mind - thanks for highlighting this! But do we need to make sure that we cater for all possible scenarios today or do we just document the current behavior that "might" affect users when building mixed source projects?

b) Add "--undefined _QQmain" to the link line.

What if the _QQmain is really missed for some reason?

With and without "--undefined _QQmain", users will see a linker error if __QQmain is missing (and they are trying to generate an executable).

When "--undefined SYMBOL" is passed, linking actually succeeds even when SYMBOL is missing. SYMBOL will then be an undefined symbol in the linked object, but will not make much harm as long as it is not referenced.

Yes, so when _QQmain is needed, "--undefined SYMBOL" solves the problem. When _QQmain is not needed, it does no harm, right?

So, under option b), a few situations can arise:

* No main, fortran nor c, is provided. libFortran_main.a (which exports main and imports _QQmain) is linked in, and linking fails as _QQmain is not resolved.

Just to clarify, when linking "libFortran_main.a", main is provided. Linking will fail because none of the Fortran object files exports _QQmain. That's a user error to me (i.e. in Flang you can only generate executable from object files that export _QQmain).

* A fortran main program is defined in an object file or archive. It will define _QQmain, which will be called by main of libFortran_main.a, and be the entry point of the program.

All good here! Just to clarify, a Fortran object file will define _QQmain. main will be defined in libFortran_main.a.

* A c main function is defined in an object file or archive. It will be the entry point of the program, libFortran_main.a will not be linked in as main is already defined. _QQmain will remain an unresolved symbol in the executable, but linking will succeed.

Note that this is a scenario that's currently neither tested nor officially supported. In any case, if main is to come from a C object, then to me it sounds like the linker doesn't need _QQmain and everything seems fine.

* We are building an so/dll/dylib, with no main in the user code. libFortran_main.a will not be linked in since main is not expected in a shared object. _QQmain will remain undefined in the so, but linking will succeed. Given option b), I would suggest flang-new does not pass "--undefined _QQmain" in this situation.

Building a shared object and an executable leads to a different linker invocation in the driver. "--undefined _QQmain" would only be added when linking an executable. All good here.

* Both a c and a fortran main are defined in user code. main will already be defined, so libFortran_main.a is not linked in. _QQmain will be resolved by the fortran main program, but will not be called from anywhere as libFortran_main.a is missing. The c main will be the entry point of the executable (which will link successfully). _This situation is a bit unfortunate, I would rather see linking failing here._

This is a tricky situation and IMO requires more wider discussion first. Folks would need to decide which implementation of main takes precedence here. To me, providing 2 implementations of main should lead to an error. But, I would consult GFortran and the community first. Based on that we can implement the necessary logic.

All of this is in a linux context. I do not know what will happen on windows, but I would bet it will be different ...

Yes, both Windows and Darwin (and other platforms) will be different and the implementation in Clang is separate. Lets solve this problem on one platform first and make sure that the solution is well documented/tested.

To me, it still sounds like "--undefined _QQmain" is a good solution here. I agree that aligning with GFortran (i.e. option c)) would be better and I would support that. But do we have a volunteer to implement that? :)

Two additional points:

  • Catering for mixed Fortran/C projects is both relevant and important, but doesn't have to be solved in this ticket. From what I can see, adding "--undefined _QQmain" to the linker invocation is not going to break anything here (which makes it an attractive option).
  • The term "Fortran main" is a bit ambiguous and unclear to me. main can either come from C or "libFortran_main.a". Flang object files do no define main, but _QQmain instead. "Fortran main" could mean (i.e. in the context of Flang) main (from "libFortran_main.a") or _QQmain (from a Fortran object file) or both. We could disambiguate things by sticking to actual object file symbols (i.e. main and _QQmain).

Right, hopefully this time I got things right :) Thanks again for digging into this!

-Andrzej

banach-space avatar Aug 26 '22 09:08 banach-space

[...] But do we need to make sure that we cater for all possible scenarios today or do we just document the current behavior that "might" affect users when building mixed source projects?

I think it is good to have mixed projects in mind, and facilitate them where it is possible without too much effort. Favoring b) over a) is a clear example I think -- a) makes a quite realistic scenario (c-main) impossible, which b) makes possible with a modest extra implementation effort. Going all the way to c) is a different thing -- it may facilitate more use cases, but the extra effort is considerable.

Yes, so when _QQmain is needed, "--undefined SYMBOL" solves the problem. When _QQmain is not needed, it does no harm, right?

As far as I understand, yes. If you don't count the (small) harm of exposing an unused unresolved symbol.

[main defined in c] Note that this is a scenario that's currently neither tested nor officially supported. In any case, if main is to come from a C object, then to me it sounds like the linker doesn't need _QQmain and everything seems fine.

I tried this with a very simple example. It works, and will still work if b) is implemented. We should add some simple tests for this.

* Both a c and a fortran main are defined in user code. main will already be defined, so libFortran_main.a is not linked in. _QQmain will be resolved by the fortran main program, but will not be called from anywhere as libFortran_main.a is missing. The c main will be the entry point of the executable (which will link successfully). _This situation is a bit unfortunate, I would rather see linking failing here._

This is a tricky situation and IMO requires more wider discussion first. Folks would need to decide which implementation of main takes precedence here. To me, providing 2 implementations of main should lead to an error. But, I would consult GFortran and the community first. Based on that we can implement the necessary logic.

I also think the linker should throw an error here, this is a scenario where b) does not reach all the way. But in my imagination, this should be a quite uncommon user error ...

To me, it still sounds like "--undefined _QQmain" is a good solution here. I agree that aligning with GFortran (i.e. option c)) would be better and I would support that. But do we have a volunteer to implement that? :)

I think we have reached a common understanding. c) looks like the best option long term, but it is many bucks for not so much more bang than what we would get for the considerably cheaper b). So I agree, b) appears like a good compromise, for now at least. I can try to prepare a patch for that (including the documentation and tests we talked about).

@PeixinQiao, does this sound ok to you? For the time being, you should be able to build the test case from the issue summary with

flang-new -Wl,--undefined=_QQmain a.a -o a.out -flang-experimental-exec

Emil

ekieri avatar Aug 28 '22 18:08 ekieri

Thanks @ekieri and @banach-space . Sound good to me. @ekieri Please feel free to prepare the patch.

PeixinQiao avatar Aug 29 '22 14:08 PeixinQiao

https://reviews.llvm.org/D134821

ekieri avatar Sep 28 '22 19:09 ekieri

Apologies in advance if this is slightly off topic, but I happened across this through the review above and had questions related to the discussion here.

I think this issue is also something I have to grapple with at some point for the -fconvert patch I am trying to merge (issue here, review here) given that I am trying to add an extern _QQEnvironmentDefaults to Fortrain_main.c, similar to _QQmain. So, I think (and apologies if I am misunderstanding!) I would also need to add an --undefined=_QQEnvironmentDefaults to avoid retriggering this issue. Additionally, option c) described above came up in the -fconvert review as well (please see the discussion here) as it would allow me to avoid defining _QQEnvironmentDefaults in the first place. However, in that review we also decided that, while ideal, it would require significant changes.

What I'm wondering is, given that (I think) -fconvert will also need the --undefined flag and since c) has popped up at least twice now (that I am aware of), does that start to change the "bang for buck" imbalance mentioned above? I don't want to derail or block the patch @ekieri posted, but I might be able to eventually try to take on implementing c) if this issue + the issues -fconvert brings means the community would be ok with moving away from the current Fortran_main.c setup. Is there a good place to bring up option c) to get additional feedback on it?

Again, I don't want to derail/block the patch above--to me, merging @ekieri's fix and (eventually) handling -fconvert's _QQEnvironmentDefaults similarly seems best to me for now--I'm just thinking this might be something I can eventually come back to if it seems reasonable. Thanks!

jonathonpenix avatar Sep 28 '22 22:09 jonathonpenix

So, I think (and apologies if I am misunderstanding!) I would also need to add an --undefined=_QQEnvironmentDefaults to avoid retriggering this issue.

@jpenix-quic I don't think you need to worry about it for the -fconvert= option. The -fconvert= option operates on main program, but users usually don't make it an archive (main.a? this is odd usage) from one object file (main.o), at least I don't see any workload doing that. The common way is as follows for the main program:

$ flang main.f90 -o main.o -fconvert=...
$ flang main.o some.a ... -l...

In addition, there will be no extern _QQEnvironmentDefaults in the long term. So, I prefer to avoid doing it.

PeixinQiao avatar Sep 29 '22 01:09 PeixinQiao

@jpenix-quic , if you find the time to implement c), that would be great! My hack solves this particular issue, but it is not very beautiful, and I would be happy to bin it when/if c) is available. I would suggest an RFC on Discourse to get some in-advance feedback.

ekieri avatar Sep 29 '22 19:09 ekieri

I think that everyone is in favor of c), but we have no volunteers to work on it :( I will also raise it internally.

banach-space avatar Sep 29 '22 20:09 banach-space

@PeixinQiao Sounds good, I won't worry about this for fconvert for now. Thanks for checking on this/clarifying this for me!

@banach-space I'd be willing to try to take this on if someone else doesn't get to it first, but it will be a little while before I can (and disclaimer that I'm still a bit new to everything!).

@ekieri Sounds good regarding getting feedback from Discourse, thanks!

jonathonpenix avatar Sep 29 '22 23:09 jonathonpenix

@llvm/issue-subscribers-bug

llvmbot avatar Jun 06 '23 17:06 llvmbot

The issue as reported was fixed by https://github.com/llvm/llvm-project/commit/876df74dd47196a9ca3b4fff21ffb5441491a0a0.

$ /home/david.spickett/LLVM-20.1.8-Linux-ARM64/bin/flang case.f90 -c
$ ar -r a.a case.o
ar: creating a.a
$ /home/david.spickett/LLVM-20.1.8-Linux-ARM64/bin/flang a.a -o a.out
$

DavidSpickett avatar Jul 22 '25 08:07 DavidSpickett