Michael Tautschnig <mt@debian.org> writes:
I don't believe there is undefined behaviour in this case.
Details below. Regardless, fixing this would make the sources
easier to understand.
The linker does not link all the members of libwmlscript.a into
the output file, only those that satisfy undefined references.
So although libwmlscript.a(wmlsc.o) and libwmlscript.a(wmlsdasm.o)
both define a main function, the linker links neither of those
because it already has a main function from the wmlsc.o that was
specified in the command line, and those members do not satisfy
any other undefined references either.
libwmlscript.a(wmlsdasm.o) also defines a lookup_function symbol.
If you add the -Wl,--undefined=lookup_function option to the gcc
command that links wmlscript/wmlsc from wmlscript/wmlsc.o,
libwmlscript.a, and other archives, then the linker will link
libwmlscript.a(wmlsdasm.o) in order to satisfy the reference to
lookup_function. That then causes an error:
libwmlscript.a(wmlsdasm.o): In function `main':
/home/kalle/build/kannel/wmlscript/wmlsdasm.c:98: multiple definition of `main'
wmlscript/wmlsc.o:/home/kalle/build/kannel/wmlscript/wmlsc.c:115: first defined here
collect2: error: ld returned 1 exit status
Because that error does not normally occur during the build, I think it
is clear that the linker normally links neither libwmlscript.a(wmlsc.o)
nor libwmlscript.a(wmlsdasm.o) into the output file wmlscript/wmlsc.
If you instead removed wmlscript/wmlsc.o from the command line,
then the linker would try to find a main function in the archives,
and I think it could choose either libwmlscript.a(wmlsc.o) or
libwmlscript.a(wmlsdasm.o). I think that would be undefined behaviour.