problem with linkage (ld segfault)

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kubawal
Member
Member
Posts: 26
Joined: Thu Mar 06, 2014 9:14 am

problem with linkage (ld segfault)

Post by kubawal »

Hi!

My system is Windows XP ( :) yeah, I know that it's old but my computer can't run newer software :( ).
I have two VM's (on Virtual Box): first with Linux and second with my OS.
I write code on Windows, save it into shared folder with Linux VM. Next I compile it on first VM using bash script (look down) and I create bootable image using
grub-mkrescue. Finally I mount it to second VM and run it.

It was working properly until I upgraded binutils (to v2.22). When I try to link everything into one binary ld crashes with segfault. But when I link to elf format, it doesn't crash
I also tried to linkage to elf format and later convert it into binary using objcopy, but when run it i does nothing (black screen) (earlier it shows messages from system loading)

My script:

Code: Select all

#!/bin/bash

#### data

# kod C++
cppSrc=( "kernel.cpp" "Driv/interrupt.cpp" "Driv/textmode.cpp" "API/string.cpp" "Driv/keyboard.cpp" "Driv/memory.cpp" "Driv/ata.cpp" "Shell/shell.cpp" "Int/systemInts.cpp" "Int/exc.cpp" "gdt.cpp" "Driv/PIT.cpp" "Driv/filesystem.cpp" "Driv/fatFs/ff.c" "Driv/bget/bget.c" )

cppOut=( "kernel.o" "Driv/interrupt.o" "Driv/textmode.o" "API/string.o" "Driv/keyboard.o" "Driv/memory.o" "Driv/ata.o" "Shell/shell.o" "Int/systemInts.o" "Int/exc.o" "gdt.o" "Driv/PIT.o" "Driv/filesystem.o" "Driv/fatFs/ff.o" "Driv/bget/bget.o" )
gccOpts="-c -nostdinc -fomit-frame-pointer -O2 -std=c++11 -I API/"

# asm
asmSrc=("boot.asm" "API/asmAPI.asm" "Int/intDefs.asm" "Int/exc.asm" )
asmOut=("boot.o" "API/asmAPI.o" "Int/intDefs.o" "Int/excasm.o" )

# linking
linkScript="link.ld"

# wyjście
isoFolder="distr"
out="distr/boot/grub/myos.bin"
outIso="MyOS.iso"
temp="temp.run"

#### executing

# asm
for((count=0; count<${#asmSrc[*]}; count++))
do
    if [ -f ${asmOut[$count]} ]
	then
		# skipping
	else
nasm -f elf -o ${asmOut[$count]} ${asmSrc[$count]}	

	fi
done

# c++
for((count=0; count<${#cppSrc[*]}; count++))
do
    if [ -f ${cppOut[$count]} ]
	then
		echo -e "\e[34mAlready compiled. Skipping\e[1m" ${cppSrc[$count]} "\e[0m"
	else	
		echo -e "Kompilacja: \e[1m" ${cppSrc[$count]} "\e[0m"
		if g++ -o ${cppOut[$count]} ${cppSrc[$count]} $gccOpts; then
			echo -e "\e[34mCompiled\e[1m" ${cppSrc[$count]} "\e[0m"
		else
			exit 1;
		fi
	fi
done

ld -T $linkScript -o $temp ${asmOut[*]} ${cppOut[*]}
objcopy -O binary -j .text $temp $out

# clean objs
	for((count=0; count<${#asmOut[*]}; count++))
	do
		rm ${asmOut[$count]}
	done
	
	for((count=0; count<${#cppOut[*]}; count++))
	do
		rm ${cppOut[$count]}
	done
grub-mkrescue --output=$outIso $isoFolder

Have you ever had problem like this?
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: problem with linkage (ld segfault)

Post by Octocontrabass »

kubawal wrote:

Code: Select all

g++
It probably doesn't work because you're not using a cross-compiler.
kubawal
Member
Member
Posts: 26
Joined: Thu Mar 06, 2014 9:14 am

Re: problem with linkage (ld segfault)

Post by kubawal »

I have installed binutils (on Linux)
User avatar
hometue
Member
Member
Posts: 100
Joined: Thu Dec 19, 2013 1:40 am
Location: Asia, Singapore

Re: problem with linkage (ld segfault)

Post by hometue »

Having binutils installed wouldn't be enough. The purpose of a cross-compiler mainly is to target the system without the need for things in between to handle them...like the one on your linux installation probably targets your linux distro and to run whatever binary it creates you will need the linux standard libraries (or what do you call them, I may be wrong in the name). An empty system definitely do not have them, so what you need is to follow the instructions on the wiki in order to generate binaries that you can use to make your OS.

P.S: this maybe slightly inaccurate or very brief, but do understand this: the binutils and compiler you have in your linux distro is different from the one you use when osdeving, they have different targets.
CookieOS. Want a cookie? Its only black and white for now though, probably as bad as my baking skills.
kubawal
Member
Member
Posts: 26
Joined: Thu Mar 06, 2014 9:14 am

Re: problem with linkage (ld segfault)

Post by kubawal »

If you say so, why eariler it works?
Octocontrabass
Member
Member
Posts: 5604
Joined: Mon Mar 25, 2013 7:01 pm

Re: problem with linkage (ld segfault)

Post by Octocontrabass »

Luck?

Usually people run into problems much earlier than you did.
kubawal
Member
Member
Posts: 26
Joined: Thu Mar 06, 2014 9:14 am

Re: problem with linkage (ld segfault)

Post by kubawal »

And you say that I haven't got cross-compiler is reason of segfault? I don't belive it
I'll try to compile cross-compiler but I think that it will not change anything...
User avatar
sortie
Member
Member
Posts: 931
Joined: Wed Mar 21, 2012 3:01 pm
Libera.chat IRC: sortie

Re: problem with linkage (ld segfault)

Post by sortie »

Using a cross-compiler will solve other problems than the one you are currently experienced, and perhaps also the problem you are experiencing (if you are lucky). Note that Bintuils 2.22 is not the latest release and that Binutils 2.24 is the latest release. Use the latest binutils.

If you continue to have this problem when you use a cross-compiler (which you likely will, but using a cross-compiler is good for other reasons), you need to give us more information. First of all, don't give us your build script, or a Makefile for that matter. You need to give us the actual commands that were invoked, not commands wrapped in all sorts of shell scripting logic. You can use the -x option to bash to make it dump the executed commands prefixed with the value of $PS4. For a Makefile, you can paste the actual output, or use the -n option to GNU make. You need then to provide us with the real output you get when the alleged segmentation fault happens. You may wish to use the -e option when invoking your build script and possibly writing "set -e" in the build script to enable the bash mode where the shell script exits when an error occurs (rather than continuing).

The best method is linking (using your cross-gcc rather than ld) to ELF, and then using objcopy to convert to a `flat binary'. Note that flat binaries are a _terrible_ and _stupid_ format because they don't contain information about where they are loaded and sequences of no program data can become a lot of zeros in the flat binary. If you lied in the linker script about where the flat binaries will be loaded, then you end up with weird problems.
kubawal
Member
Member
Posts: 26
Joined: Thu Mar 06, 2014 9:14 am

Re: problem with linkage (ld segfault)

Post by kubawal »

sortie wrote:The best method is linking (using your cross-gcc rather than ld) to ELF, and then using objcopy to convert to a `flat binary'.
As you may see in first post I try to link to ELF (OUTPUT_FORMAT("elf32-i686") in link script) and use objcopy (but I don't know whether I do it right) - read first post.
Post Reply