Linking Non-Executable Intermediate Files Together

Programming, for all ages and all languages.
Post Reply
Codesmith
Posts: 3
Joined: Wed May 06, 2015 12:13 pm

Linking Non-Executable Intermediate Files Together

Post by Codesmith »

Right now I've got a minimal OS up and running, so before I move on I decided to work on my build environment (cleanliness is godliness).

Right now I'm almost satisified with how things build, here's a quick overview

./build.sh
- Passes control to /stdcpp/build.sh, which builds my "standard" library (it's not really standard if its mine :P)
-- If an error occours, build stops and prints out errors
- Passes control to /kernel/build.sh, which builds my kernel, linking to all required .o files

.asm are compiled to .ao, .c to .o, and .cpp to .opp. If globally usable, files are placed into a ./out/ directory.

The problem is that I've got a library file that needs both a .cpp and .asm. I can get it to work by linking the kernel to both the .ao and .opp, but I'd rather combine the .ao and .opp so that I can have full functionality while only linking to one file. (This would maintain the globally usability)

The concept is to compile the parts in the source structure, link the resulting parts, and send the single, usable output to ./out/

I gave it a try by running the following command

Code: Select all

i686-elf-ld stdo.ao stdo.opp -o ${BUILD}/out/stdo.lo -ffreestanding -O2 -nostdlib -shared
std.ao and std.opp are results of asm and cpp portions that cannot stand alone. std.lo is the output in which they are linked together, so it can stand alone.

Unfortunately doing this builds fine, but it breaks the OS, so I'd imagine that I'm missing something low-level.

TLDR I've got two static library files resulting from nasm and gcc that need to be linked together to be used. Since they aren't executable I cannot (I assume) link using

Code: Select all

gcc -T linker.ld -o file.elf -ffreestanding -O2 -nostdlib file.o file1.o -lgcc
that I picked up from the barebones tutorial.

How do I go about linking them together so I can then link to the single file everywhere it is used?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Linking Non-Executable Intermediate Files Together

Post by gerryg400 »

Can't you use i686-elf-ar to link arbitrary object files together into a library ?
If a trainstation is where trains stop, what is a workstation ?
Codesmith
Posts: 3
Joined: Wed May 06, 2015 12:13 pm

Re: Linking Non-Executable Intermediate Files Together

Post by Codesmith »

I'm not very familiar with the gcc toolchain, thanks for the pointer!

For reference, the final command was

Code: Select all

i686-elf-ar -q archive.lo input.ao input2.opp
Where archive.lo is the resulting "linked" file, and input.ao / input2.opp are the input independent files.
Kevin
Member
Member
Posts: 1071
Joined: Sun Feb 01, 2009 6:11 am
Location: Germany
Contact:

Re: Linking Non-Executable Intermediate Files Together

Post by Kevin »

Using ar is the right way to produce a static library, but I just want to add something to the original question on how to combine two object files into one: I think you'd want the -r option for ld, and probably -shared is the reason why it didn't work any more (unless you really support shared libraries in your "minimal" OS).
Developer of tyndur - community OS of Lowlevel (German)
Post Reply