Direct use of ld (was "Problem with gnu linker")

Programming, for all ages and all languages.
Post Reply
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Direct use of ld (was "Problem with gnu linker")

Post by Love4Boobies »

I'd like to add that, as a matter of good practice, ld shouldn't be used by the programmer directly. It was designed to be the back end for GCC and should be used via GCC.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Problem with the gnu linker (ld.exe)

Post by Solar »

Love4Boobies wrote:I'd like to add that, as a matter of good practice, ld shouldn't be used by the programmer directly. It was designed to be the back end for GCC and should be used via GCC.
Aside from our Bare Bones tutorial disagreeing with you, what makes you think he did so? Even if used through GCC, the PE error message would be generated by ld. (Or would it not?)
Every good solution is obvious once you've found it.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Problem with the gnu linker (ld.exe)

Post by JamesM »

Love4Boobies wrote:I'd like to add that, as a matter of good practice, ld shouldn't be used by the programmer directly. It was designed to be the back end for GCC and should be used via GCC.
Well no, it's not. It's not even distributed in the same package as GCC.
User avatar
iansjack
Member
Member
Posts: 4687
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with the gnu linker (ld.exe)

Post by iansjack »

So I need to install gcc if I want to do pure assembler programming? Or maybe I can just write my own linker.
User avatar
NickJohnson
Member
Member
Posts: 1249
Joined: Tue Mar 24, 2009 8:11 pm
Location: Sunnyvale, California

Re: Problem with the gnu linker (ld.exe)

Post by NickJohnson »

iansjack wrote:So I need to install gcc if I want to do pure assembler programming? Or maybe I can just write my own linker.
... binutils is not GCC. GCC depends on binutils, but not vice versa. GNU ld and as are part of binutils.
User avatar
iansjack
Member
Member
Posts: 4687
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Problem with the gnu linker (ld.exe)

Post by iansjack »

Sorry, the sarcastic nature of my post wasn't clear enough. I am well aware that ld is nothing to do with gcc, just a tool that it uses.

I am amused by the presumption that a programmer cannot be trusted to use the tool directly. I'm guessing that next I'll be warned about using objdump - why on earth would I want to know what is actually in my object files? (Before anyone takes that rhetorical question seriously - don't.)
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Problem with the gnu linker (ld.exe)

Post by Love4Boobies »

Solar wrote:Aside from our Bare Bones tutorial disagreeing with you,
If the Bare Bones tutorial disagrees with me, I stand corrected. :lol:
Solar wrote:what makes you think he did so? Even if used through GCC, the PE error message would be generated by ld. (Or would it not?)
You are right about this one. I was under the impression that ld's error code was used by GCC in printing the linking error message but this is apparently not true so I can't really know it's what he was doing at all.
JamesM wrote:
Love4Boobies wrote:I'd like to add that, as a matter of good practice, ld shouldn't be used by the programmer directly. It was designed to be the back end for GCC and should be used via GCC.
Well no, it's not. It's not even distributed in the same package as GCC.
It doesn't really matter how it's distributed. I don't know whether this still holds, but glibc used to be distributed as a separate package from GCC even if it was designed to be used only with GCC. Furthermore, GCC provides a way to pass arguments to ld via its command line specifically because the developers of GCC are aware that GCC is meant to be the front end. Last but not least, think of POSIX for a second---it deprecated ld utility in favour of using the same utility for both compiling and linking, which means implementations can still use ld as a back end if they so wish.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Direct use of ld (was "Problem with gnu linker")

Post by Combuster »

I'm well aware that your the solo endeavour to promote host gcc for crosscompiling because it apparently works and happens to support ELF. But any form of apparent authority on this one got lost after reading this:
Love4Boobies wrote:I was under the impression that ld's error code was used by GCC in printing the linking error message
In other words, this looks like preaching on subjects you haven't mastered yourself.

At any rate, I
- Don't need stuff dragged in to deal with C in non-C projects
- Don't want a poorly documented channel that arranges things with the linker without my knowledge
- Want to be independent of compiler choice as GCC is not going to be the self-hosted compiler.
And for the educational value towards others, I believe that teaching people shortcuts for which they are unable to understand the underlying choices is a really, really bad idea when people trust your wisdom based on your post count.

Therefore I advice against using GCC pretending to be just the linker. In this business, you need to know what you're doing, and using ld directly enforces that practice.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
iansjack
Member
Member
Posts: 4687
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Direct use of ld (was "Problem with gnu linker")

Post by iansjack »

It doesn't really matter how it's distributed. I don't know whether this still holds, but glibc used to be distributed as a separate package from GCC even if it was designed to be used only with GCC.
Rubbish. glibc is a library, not part of a compiler suite; it's not designed just to work with gcc.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Direct use of ld (was "Problem with gnu linker")

Post by gerryg400 »

iansjack wrote:
It doesn't really matter how it's distributed. I don't know whether this still holds, but glibc used to be distributed as a separate package from GCC even if it was designed to be used only with GCC.
Rubbish. glibc is a library, not part of a compiler suite; it's not designed just to work with gcc.
You must use GCC (and binutils) to build glibc. It will not build with other toolchains. However, once the library is built, it can be linked with programs that are compiled with other toolchains.
If a trainstation is where trains stop, what is a workstation ?
User avatar
gravaera
Member
Member
Posts: 737
Joined: Tue Jun 02, 2009 4:35 pm
Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.

Re: Direct use of ld (was "Problem with gnu linker")

Post by gravaera »

Combuster wrote:...when people trust your wisdom based on your post count.
High postcount is sooo mainstream \o/
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Direct use of ld (was "Problem with gnu linker")

Post by Love4Boobies »

Combuster wrote:I'm well aware that your the solo endeavour to promote host gcc for crosscompiling because it apparently works and happens to support ELF.
It doesn't randomly happen to support ELF. It supports whichever ABI(s) your installation currently targets. I'm not trying to promote anything, but trying to save people the time required to build cross utilities in the cases they aren't actually needed---which is obviously not always.
Combuster wrote:But any form of apparent authority on this one got lost after reading this:
Love4Boobies wrote:I was under the impression that ld's error code was used by GCC in printing the linking error message
In other words, this looks like preaching on subjects you haven't mastered yourself.
Given what I've learnt from previous mistakes, I'd rather keep the discussion technical rather than to throw little insults around. Hope you will do the same. At any rate, I believe the fact that I use GCC as the front end for ld is a fair justification for not needing to know exactly how they interact.
Combuster wrote:At any rate, I
- Don't need stuff dragged in to deal with C in non-C projects
But this isn't really inherent to GCC. After all, GCC does support other languages as well.
Combuster wrote:- Don't want a poorly documented channel that arranges things with the linker without my knowledge
There are two things that fall under this category: startup files and libraries. Each has a command-line option, and there's one that either turns both on or off.
Combuster wrote:- Want to be independent of compiler choice as GCC is not going to be the self-hosted compiler.
This is a fair point; I guess I was thinking the reverse case: easily using GCC with any linker. GCC has command-line options for using another linker and/or pass additional flags to the linker.

But I don't want to avoid answering your concern entirely so I will at least note that, as I've mentioned above, POSIX defines utilities that are both compilers and the link editors. Hence, one can at least aspire for independence in compliant environments. (It's true that POSIX only does this for C and Fortran, but consistency is a pretty safe assumption, I would say.)
Combuster wrote:And for the educational value towards others, I believe that teaching people shortcuts for which they are unable to understand the underlying choices is a really, really bad idea when people trust your wisdom based on your post count.
That's generally true. However, the question is whether these really are shortcuts or whether the community has simply just settled on a particular practice and is now suffering from the NIH syndrome. I'm not trying to point any fingers; just saying that I do consider my way of doing things more elegant rather than a hack. While elegance may be a subject for debate, I believe the use of the word shortcut to be unjust here.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Direct use of ld (was "Problem with gnu linker")

Post by Combuster »

Which pretty much proves the point that GCC can not be used as a linker without dealing with the compiler part specifically.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: Direct use of ld (was "Problem with gnu linker")

Post by Love4Boobies »

Sure, think of it as encapsulation. Besides, I find that the number of cases where this is the cleaner solution (and thus simpler) outweighs the cases where one has to deal with turning off compiler switches that are typical of hosted environments.

Given that I'm not imposing my views on anyone, I think it's okay for me to recommend the things that I consider to be better even if they may disagree with your views.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply