Linking GCC objects with ld

Linking GCC objects with ld

Post by gerson ku » Tue, 28 Nov 2000 04:00:00



Sorry, this is a beginners question, but is there a reference document
for the libraries and issues involved with linking GCC created objects
with ld ? The GCC manual does not say anything about it, and "man ld"
was of not much help, either. Specifically, I have the following
questions

- which is the correct startup code ? I find on my system
/usr/lib/bcc/crt0.o, /usr/lib/crt1.o, /usr/lib/crti.o,
/usr/lib/crtn.o, plus suspiciously named crtbegin.o.

- are there runtime libs made specifically for multithreaded programs
? on other OS, you have to link special libraries if your code is
multithreaded, because of the synchronisation issues involved (e.g.
accessing strtok() from two threads and things like that), or will
libg++.so do for all kinds of targets ?

- Is there a tool that will check dynamically loaded libraries for
their existence and location ? (e.g. CHKDLL32 on OS/2, DEPENDS on NT)
?

- what goes wrong if ld creates an executable ("test") with
/usr/lib/crt1.o without complaining, and when I start it using ./test
I will get

bash: ./test: No such file or directory

when I try "file", I get

file: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically
linked (uses shared libraries), not stripped

 
 
 

Linking GCC objects with ld

Post by Martin Schmid » Tue, 28 Nov 2000 04:00:00


Quote:> - what goes wrong if ld creates an executable ("test") with
> /usr/lib/crt1.o without complaining, and when I start it using ./test
> I will get

> bash: ./test: No such file or directory

Never use test as a name for your program ,
because there is already  a program named test
on every linux system .

 
 
 

Linking GCC objects with ld

Post by gerson ku » Tue, 28 Nov 2000 04:00:00


On Mon, 27 Nov 2000 12:56:52 +0100, "Martin Schmidt"


>Never use test as a name for your program ,
>because there is already  a program named test
>on every linux system .

I have asked the linker to name the file

- pmake (which as I found out also already exists), then
- pmakex
- pmakex.exe
- test
- test.exe

all files where created, none of these could be started.

 
 
 

Linking GCC objects with ld

Post by jacob navi » Tue, 28 Nov 2000 04:00:00


You can't use the linker under linux
Forget all the man pages and all documentation.
I have never been able to use the linker without gcc

The gcc driver calls an undocumented script called "collect2" that lives
somewhere under /usr/lib.
That script will make several transformations of the object files to arrange
for gnu gcc internals, then call ld, then do some post processing.

All this is completely undocumented and changes from minor version of gcc to
the next one

By the way when you read
"./test: no such file or directory"

this means (in linux englisch)
"./test: some dll wasn't found and I will not tell you which"

This message is written to the console when the loader doesn't find a dll,
or in another circumstances, for instance when the executable is corrupt or
other problems.

Sorry about the terminology by the way. dll is a "windows" hangover I have
still: the correct name in linux terminology is "shared object" and those
have a .so suffix.

The concepts behind are quite similar, and the problem is the same.

When this error happens under windowsXX you see a message box with:
"The dll XXX could not be found in:(PATH variable)"

linux outputs a meaningless error message.


> On Mon, 27 Nov 2000 12:56:52 +0100, "Martin Schmidt"

> >Never use test as a name for your program ,
> >because there is already  a program named test
> >on every linux system .

> I have asked the linker to name the file

> - pmake (which as I found out also already exists), then
> - pmakex
> - pmakex.exe
> - test
> - test.exe

> all files where created, none of these could be started.

 
 
 

Linking GCC objects with ld

Post by Martin Knoblauc » Tue, 28 Nov 2000 04:00:00


what happens, when you try to start them? Have you tried to prepend "./"?

Martin


> On Mon, 27 Nov 2000 12:56:52 +0100, "Martin Schmidt"

> >Never use test as a name for your program ,
> >because there is already  a program named test
> >on every linux system .

> I have asked the linker to name the file

> - pmake (which as I found out also already exists), then
> - pmakex
> - pmakex.exe
> - test
> - test.exe

> all files where created, none of these could be started.

 
 
 

Linking GCC objects with ld

Post by gerson ku » Tue, 28 Nov 2000 04:00:00


On Mon, 27 Nov 2000 12:00:04 -0800, "jacob navia"


>You can't use the linker under linux
>Forget all the man pages and all documentation.
>I have never been able to use the linker without gcc

Well, I have to say you're right. I have just ported my very first app
from win32/os2 to linux, and I am linking with gcc. I think you had
some points when you started that rant about the poor linux
documentation some days ago...

One question: It seems that GCC will open and write back the files. At
least, when I have a file open in MSDEV on a machine and do a remote
compile with linux, I get a message "external file changed" in MSDEV.
Can this be turned of ? The fatal thing was that GCC crashed on one
source with internal compiler error, and changed the size OF MY
SOURCECODE to 0 bytes. Thank god I do regular backups.

 
 
 

Linking GCC objects with ld

Post by Martin Knoblauc » Tue, 28 Nov 2000 04:00:00


Gerson,

 in short, the message is: use gcc (or g++ in case of C++ code, or g77 ...)
to link the final image. It is just easiser. The longer answer seems to be
for building "bla" out of  a simple "bla.o":

ld  -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
/usr/lib/gcc-lib/i486-linux/egcs-2.91.66/crtbegin.o bla.o
 -warn-common -lc /usr/lib/gcc-lib/i486-linux/egcs-2.91.66/crtend.o
/usr/lib/crtn.o -o bla

 All of it seems to be "vital". If you leave out "-dynamic-linker", you get
your "file not found message". If you leave out other parts, you get
anything from core dumps to "access denied". All without error messages from
"ld". It seems, the error reporting for "illegal" executables leaves a lot
to desire :-)

 "collect2" ins not neccessarily the issue here. You may need it if you want
to debug your exe. Anyway see "short message" :-) Or just suffer :-))

Martin


Quote:> Sorry, this is a beginners question, but is there a reference document
> for the libraries and issues involved with linking GCC created objects
> with ld ? The GCC manual does not say anything about it, and "man ld"
> was of not much help, either. Specifically, I have the following
> questions

> - which is the correct startup code ? I find on my system
> /usr/lib/bcc/crt0.o, /usr/lib/crt1.o, /usr/lib/crti.o,
> /usr/lib/crtn.o, plus suspiciously named crtbegin.o.

> - are there runtime libs made specifically for multithreaded programs
> ? on other OS, you have to link special libraries if your code is
> multithreaded, because of the synchronisation issues involved (e.g.
> accessing strtok() from two threads and things like that), or will
> libg++.so do for all kinds of targets ?

> - Is there a tool that will check dynamically loaded libraries for
> their existence and location ? (e.g. CHKDLL32 on OS/2, DEPENDS on NT)
> ?

> - what goes wrong if ld creates an executable ("test") with
> /usr/lib/crt1.o without complaining, and when I start it using ./test
> I will get

> bash: ./test: No such file or directory

> when I try "file", I get

> file: ELF 32-bit LSB executable, Intel 80386, version 1, dynamically
> linked (uses shared libraries), not stripped

 
 
 

Linking GCC objects with ld

Post by Gerson Ku » Tue, 28 Nov 2000 04:00:00


On Mon, 27 Nov 2000 15:06:06 +0100, "Martin Knoblauch"


> in short, the message is: use gcc (or g++ in case of C++ code, or g77 ...)
>to link the final image. It is just easiser. The longer answer seems to be
>for building "bla" out of  a simple "bla.o":

>ld  -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o
>/usr/lib/gcc-lib/i486-linux/egcs-2.91.66/crtbegin.o bla.o
> -warn-common -lc /usr/lib/gcc-lib/i486-linux/egcs-2.91.66/crtend.o
>/usr/lib/crtn.o -o bla

Well, I've tried gcc and it works. Still I would like to be able to
know what goes on here, because see the first project I (successfully
I may proudly say) ported was my own make tool with autodependencies
(I hate it: its the year 2000 and I have to manually specify
dependencies in makefiles, and AUTOMAKE is so obfuscated there exists
a whole book on just using it, and MAKE is used as a very lousy
installer & configurer instead of just focusing on its main job:
building targets, and it reminded me much of the dreaded NMAKE I'm
refusing to work with for years. You know, you change the source to
include a new file and you have to run AUTOMAKE to generate
dependencies, and then MAKE to generate the file, and isn't that just
inefficient).

So, I ported the make tool I wrote for both OS/2 and Win32 to X. I can
build apps with it (at least: it can build itself; using gcc as the
linker), and the dependencies checker works fine, but I still have a
bad feeling about multithread support.

I did not find any GCC options for generating multithreaded code, so
the question is really this: is the GLIBC or whatever GCC will link my
app with (since I don't know: GCC decides which libs to use)
multithread-safe ? In all stdc libs I have looked at (like IBM Visual
Age, and Borland C++, and Microsofts Visual C++) some functions need
to be semaphore protected when used in multithreaded environment. In a
singlethreaded environment that would be a performance downgrade.

 
 
 

Linking GCC objects with ld

Post by Paul Kimo » Tue, 28 Nov 2000 04:00:00



> I have asked the linker to name the file

> - pmake (which as I found out also already exists), then
> - pmakex
> - pmakex.exe
> - test
> - test.exe

> all files where created, none of these could be started.

Why do you want to invoke ld(1) directly?  For normal use, you should ask
the compiler to invoke the linker: it knows whatever magic incantations
are required.  The compiler will tell you how it is invoking ld (or
possibly the linker-front-end collect2), though, if you add the "-v" flag.

--
Paul Kimoto
This message was originally posted on Usenet in plain text.  Any images,
hyperlinks, or the like shown here have been added without my consent,
and may be a violation of international copyright law.

 
 
 

Linking GCC objects with ld

Post by Floyd Davidso » Tue, 28 Nov 2000 04:00:00



>> - what goes wrong if ld creates an executable ("test") with
>> /usr/lib/crt1.o without complaining, and when I start it using ./test
>> I will get

>> bash: ./test: No such file or directory

>Never use test as a name for your program ,
>because there is already  a program named test
>on every linux system .

As long as he invokes it using ./test it makes no difference if
there are 6 other programs with the same name.  Clearly Martin
indicated, it is confirmed by the error message, that he did
invoke it that way.

The easy way to link an object file to make an executable is
to let gcc do all of the work.   "gcc test.o -o test" for
example.  But if you want to know what gcc did, such as
how it invoked the linker in this case, use the -v option
for verbose output:  "gcc -v test.o -o test" to see what
it actually does.  Then you can try a variety of other ways
to invoke gcc (read the documentation for gcc and the linker
to get ideas) to see what the differences are.  For example,
try "gcc -v -static test.o -o test" and see what that does
differently.

--

Ukpeagvik (Barrow, Alaska)

 
 
 

Linking GCC objects with ld

Post by Chri » Tue, 28 Nov 2000 04:00:00



> So, I ported the make tool I wrote for both OS/2 and Win32 to X. I can
> build apps with it (at least: it can build itself; using gcc as the
> linker), and the dependencies checker works fine, but I still have a
> bad feeling about multithread support.

> I did not find any GCC options for generating multithreaded code, so
> the question is really this: is the GLIBC or whatever GCC will link my
> app with (since I don't know: GCC decides which libs to use)
> multithread-safe ?

Broadly, yes. Obviously you need to use the reentrant
versions of functions which use a static buffer (e.g.
strtok_r not strtok; gethostbyname_r not gethostbyname
etc.); sometimes these are less-documented than they
might be (but I see from your other postings that you've
already discovered this about developing under Linux.

--
Chris Lightfoot -- http://www.ex-parrot.com/~chris/
ignore From: line; try "chris at ex hyphen parrot dot com"
  A little inaccuracy can save tons of explanation (Saki)

 
 
 

Linking GCC objects with ld

Post by Kaz Kylhe » Tue, 28 Nov 2000 04:00:00


On Mon, 27 Nov 2000 09:42:11 GMT, gerson kurz


>Sorry, this is a beginners question, but is there a reference document
>for the libraries and issues involved with linking GCC created objects
>with ld ? The GCC manual does not say anything about it, and "man ld"
>was of not much help, either. Specifically, I have the following
>questions

>- which is the correct startup code ? I find on my system
>/usr/lib/bcc/crt0.o, /usr/lib/crt1.o, /usr/lib/crti.o,
>/usr/lib/crtn.o, plus suspiciously named crtbegin.o.

Do not use ld directly. Use the compiler front-end to do linking for you.

        gcc -o progname foo.o bar.o main.o

The gcc front end will insert all the right modules for proper linking.
You can see what it does by adding the -v parameter.

Quote:>- are there runtime libs made specifically for multithreaded programs
>? on other OS, you have to link special libraries if your code is
>multithreaded, because of the synchronisation issues involved (e.g.
>accessing strtok() from two threads and things like that), or will
>libg++.so do for all kinds of targets ?

In Linux, the GNU C library is thread-aware. When you link in the threading
library libpthread (which is done with the -pthread command line parameter to
gcc) certain functions in libc.so get overridden by functions in the threading
library.

Quote:>- Is there a tool that will check dynamically loaded libraries for
>their existence and location ? (e.g. CHKDLL32 on OS/2, DEPENDS on NT)

There is something called ``ldd'' which is invoked on executables,
and it dumps their library dependencies.

Quote:>- what goes wrong if ld creates an executable ("test") with
>/usr/lib/crt1.o without complaining, and when I start it using ./test
>I will get
>bash: ./test: No such file or directory

The cryptic no such file or directory error comes from an incorrectly linked
executable. What happens is that exec*() system call invoked by the shell to
run the program results in an error with errno being set to ENOENT. So it
doesn't mean that the file does not exist; it's just that a generic error
message is being substituted in a situation where its text is not helpful.