Assembly compiling unused routines ?

News,announcements,programming,fixes,game patches & discussions.

Moderator: troed

Post Reply
User avatar
exxos
Site Admin
Site Admin
Posts: 23737
Joined: Wed Aug 16, 2017 11:19 pm
Location: UK
Contact:

Assembly compiling unused routines ?

Post by exxos »

N00b Question :

When compiling a main program and linking into libraries. Does the entire library get compiled with the program, or only the routines which are used within that library ?
https://www.exxosforum.co.uk/atari/ All my hardware guides - mods - games - STOS
https://www.exxosforum.co.uk/atari/store2/ - All my hardware mods for sale - Please help support by making a purchase.
viewtopic.php?f=17&t=1585 Have you done the Mandatory Fixes ?
Just because a lot of people agree on something, doesn't make it a fact. ~exxos ~
People should find solutions to problems, not find problems with solutions.
User avatar
stephen_usher
Posts: 5661
Joined: Mon Nov 13, 2017 7:19 pm
Location: Oxford, UK.
Contact:

Re: Assembly compiling unused routines ?

Post by stephen_usher »

Usually the whole library gets pulled in.
Intro retro computers since before they were retro...
ZX81->Spectrum->Memotech MTX->Sinclair QL->520STM->BBC Micro->TT030->PCs & Sun Workstations.
Added code to the MiNT kernel (still there the last time I checked) + put together MiNTOS.
Collection now with added Macs, Amigas, Suns and Acorns.
mikro
Posts: 484
Joined: Mon Aug 28, 2017 11:22 pm
Location: Kosice, Slovakia
Contact:

Re: Assembly compiling unused routines ?

Post by mikro »

stephen_usher wrote: Fri Apr 29, 2022 1:32 pm Usually the whole library gets pulled in.
That's a very simplified answer.

It depends on the linker and on the format of the object files. For instance, gcc + a.out format works that way that if some function is not referenced, it is discarded. HOWEVER, many functions internally reference others so even a simple hello world takes tens of kilobytes.

IIRC PureC has a similar concept (i.e. linking only those TOS calls which are used) but not sure about Devpac.
amimjf
Posts: 87
Joined: Sun Mar 22, 2020 8:25 am

Re: Assembly compiling unused routines ?

Post by amimjf »

Hi,

Similar to the above, it's all about what linker you use. Basically the linker's job is to resolve all symbols (functions) so if you call printf it will go find that in libc (for example), of course printf internally calls malloc/scanf, etc so you get a dependency list. The linker goes and pulls in the functions till all is resolved. Historically some took the simple approach and just included everything in the library.

Normally you can tell by looking in a hex editor for what happens when you call a library routine.

GAS/LD was pretty smart even in old 2.x versions from the 90's.
User avatar
mfro
Posts: 122
Joined: Thu Dec 13, 2018 7:32 am

Re: Assembly compiling unused routines ?

Post by mfro »

mikro wrote: Fri Apr 29, 2022 3:29 pm That's a very simplified answer.

It depends on the linker and on the format of the object files. For instance, gcc + a.out format works that way that if some function is not referenced, it is discarded. HOWEVER, many functions internally reference others so even a simple hello world takes tens of kilobytes.
While generally correct for most other platforms, unfortunately, that's not the whole truth for the m68k a.out linker.
This is not capable to pull individual functions from an archive but only individual objects.

If you have several functions in a single object file in a library and the linker needs only one single function from it, it will always pull the whole object (i.e. all the functions in it, even if they are not calling each other).

On other platforms sporting the ELF object format, you can use the -ffunction-sections -Wl,--gc-sections switches that will put every function object in a separate section allowing the linker to only pull what it needs. This option is of no use with the a.out object format, so the only solution is "only one cross each", i.e. only one function per source file.
And remember: Beethoven wrote his first symphony in C.
ijor
Posts: 428
Joined: Fri Nov 30, 2018 8:45 pm

Re: Assembly compiling unused routines ?

Post by ijor »

I have been using linkers and libraries since the 80's and I don't remember a linker that was dumb enough to pull an entire library. Alcyon, the very first compiler for the ST that was used to compile TOS itself, a rather primitive compiler btw, was already smart enough to extract only the modules that were required (directly or indirectly). I didn't try the very first PC compiler, but the very early ones I used were even smarter and more optimized than Alcyon.

The whole concept of library is that it is a collection of modules that are meant to be extracted. As a matter of fact the earlier tools for managing libraries were called "archiver" and not "librarian".
http://github.com/ijor/fx68k 68000 cycle exact FPGA core
FX CAST Cycle Accurate Atari ST core
http://pasti.fxatari.com
Post Reply

Return to “SOFTWARE PROGRAMMING & DISCUSSION”