[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [MiNT] Why is order of input files important for ld?
- To: Miro Kropáček <miro.kropacek@gmail.com>
- Subject: Re: [MiNT] Why is order of input files important for ld?
- From: Vincent Rivière <vincent.riviere@freesbee.fr>
- Date: Fri, 21 Jan 2011 11:59:19 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:sender:resent-from:resent-to:resent-date :resent-message-id:resent-user-agent:x-mozilla-keys:message-id:date :from:user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=eoIAl+WxaEoM6gKCP0SFxvQ/EZaMHGWhDRwfGlHbWws=; b=s3lrv6DB0LDjyEDCs/oL9yZB9QIFRFCVu/64SLM3J5LNLUz+GQfY5TyiR0/rstztWS vCEwZ1c6IutZtKqhAonkOmJt8NHKTuYsgVqd0pChcExtTAvlQkgp521Z8m4Mk9jXRZOs DakudiKXwqkmJrRr1jUJt8dTzfngAaGV370QE=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=sender:resent-from:resent-to:resent-date:resent-message-id :resent-user-agent:x-mozilla-keys:message-id:date:from:user-agent :mime-version:to:subject:references:in-reply-to:content-type :content-transfer-encoding; b=Gmnm2C6EffmB35RM6zKr7LfXhFasWpAZKNDWoAmqkGuyusPLW2CFfTTJcjmyJF5nJZ sUZq3YXSnV43ULPOFGvoiZ9DBDsaLFQydDNCVrYGi8jRW38N8LyajQ8kvjYR932yqR2z dtRSxC7tKMI9maWDeJnYT5O6pIuYWCz9sIYaU=
- In-reply-to: <AANLkTi=dqDwT+v5mN0PXQwykb4PT-GHpROaFXTddgwvM@mail.gmail.com>
- List-help: <mailto:ecartis@lists.fishpool.fi?Subject=help>
- List-id: <mint.lists.fishpool.fi>
- List-owner: <mailto:tjhukkan@fishpool.fi>
- List-post: <mailto:mint@lists.fishpool.fi>
- List-subscribe: <mailto:mint-request@lists.fishpool.fi?Subject=subscribe>
- List-unsubscribe: <mailto:mint-request@lists.fishpool.fi?Subject=unsubscribe>
- References: <AANLkTi=dqDwT+v5mN0PXQwykb4PT-GHpROaFXTddgwvM@mail.gmail.com>
- Resent-date: Fri, 21 Jan 2011 14:13:19 +0100
- Resent-from: Vincent Rivière <vincent.riviere@freesbee.fr>
- Resent-message-id: <4D39866F.3090003@freesbee.fr>
- Resent-to: mint@lists.fishpool.fi
- Resent-user-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7
- Sender: mint-bounce@lists.fishpool.fi
- User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.13) Gecko/20101207 Thunderbird/3.1.7
Miro Kropáček wrote:
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
No mystery, this is how ld works.
I have explained many times.
Input files on the ld command line are of 2 kinds:
- .o files
- libraries (either explicit .a files or -lfoo for example)
Furthermore, GCC adds additional input files at the beginning of the
command line (startup code) and at the end (standard libraries). Also,
GCC does not call ld directly, it uses a wrapper named collect2 which is
similar. You can see the exact collect2 command line when compiling with
gcc -v.
Back to ld:
The .o files are linked into the final executable, in the order they
appear on the command line. As a result, the startup code must be the
first one. (This may not be strictly true since the extended MiNT header
allows to specify an custom entry point).
What is a library ? It is simply a collection of .o files, comparable to
an uncompressed zip file.
What happens when a library is encountered on the linker command line:
- The linker makes the list of the unresolved externals found so far (in
the .o files preceding the library on the command line).
- The linker looks inside the library to see if the unresolved externals
can be satisfied.
- When a satisfying symbol is found in some .o file inside the library,
that whole .o file is linked into the executable, just as if it was on
the linker command line, at the same position as the library.
- The other library contents are totally ignored and forgotten.
- and so on.
As a result:
- The whole contents of libraries are usually no linked into the final
executable. Only the useful parts (whole .o files).
- Don't fear to use big libraries, your executable will not become
automatically fat.
- Some libraries may have to be added multiple times at the end of the
command line (such as -lc or -lgcc) to satisfy interdependencies.
And now you know it well: use -t on the ld command line (or -Wl,-t on
the gcc command line) to see exactly what happens.
if I do it in opposite way:
ld -lm main.o math_stuff.o -o output
stuff from libm.a isn't found.
Of course.
At the beginning of the command line, the only unresolved external is
main. Since it is not provided by -lm, that library will no be linked at
all.
You found the solution: always put the libraries at the end of the
linker command line, and put them multiple times if there are
interdependencies.
Ok, I could live with that. But how it's
possible it works in linux? It's due to inlining?
Yes, probably some inlining.
The rules are the same.
Again, try -Wl,-t on Linux.
Things are well defined.
--
Vincent Rivière