Page 1 of 1

Linking Non-Executable Intermediate Files Together

Posted: Thu May 07, 2015 7:44 pm
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?

Re: Linking Non-Executable Intermediate Files Together

Posted: Thu May 07, 2015 7:48 pm
by gerryg400
Can't you use i686-elf-ar to link arbitrary object files together into a library ?

Re: Linking Non-Executable Intermediate Files Together

Posted: Thu May 07, 2015 8:03 pm
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.

Re: Linking Non-Executable Intermediate Files Together

Posted: Fri May 08, 2015 5:23 am
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).