[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [MiNT] Why is order of input files important for ld?



Alan Hourihane wrote:
On Fri, 2011-01-21 at 11:21 +0100, Miro Kropáček wrote:
Hi,


today I for 1000th time bumped into this and this time I'm not going
to rest until someone gives me damn good explanation :) For some
reason, one must always (?) link files in this order:


ld<my_obj_files>  <my library files>  for example:


ld main.o math_stuff.o -o output -lm


if I do it in opposite way:


ld -lm main.o math_stuff.o -o output


stuff from libm.a isn't found. Ok, I could live with that. But how
it's possible it works in linux? It's due to inlining? (since our math
library is broken for 68881 and if I understood it right, 68881 code
was never used).


Really, really annoying.

I believe it's because we are stuck with static libs, and the way
symbols are resolved in static libraries.

Pretty sure you're still using a one-pass linker, it looks at each file in the order you specified amd it only looks for symbols in a file if they've already been referenced by a previous file. "main" is typically a special case because it's referenced by crt.o (which is implicitly the first thing in the link). libm.a doesn't have any symbols that crt.o wants, so it gets ignored.

You could write a linker that searches all files exhaustively, but its execution time would increase exponentially with the number of files being linked. Most compiler/linker writers avoid doing this...

--
  -- Howard Chu
  CTO, Symas Corp.           http://www.symas.com
  Director, Highland Sun     http://highlandsun.com/hyc/
  Chief Architect, OpenLDAP  http://www.openldap.org/project/