GAS end of file not at end of line and Clang casting

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
sds2017
Member
Member
Posts: 61
Joined: Tue Jul 05, 2011 10:05 am

GAS end of file not at end of line and Clang casting

Post by sds2017 »

Hi I'm Jackson and am developing a simple kernel. Here's some things you might want to know before I tell you about the problem.
  • My kernel binary is in a elf32-i386 file format, I'm using the GNU assembler and Clang as the compiler, lastly my code is supposed to output 'a' to the screen
My problem is when I compile, assemble and link the build log says this
entry.s: Assembler messages:
entry.s: Error: can't open entry.o for reading: No such file or directory
main.c:4:9: warning: incompatible integer to pointer conversion assigning to
'char *' from 'int';
vidmem=0xB8000;
^~~~~~~~
1 warning generated.
ld: warning: cannot find entry symbol Start; defaulting to 0000000001000000
I'm guessing the linker can't find the Start code because it won't assemble but I couldn't figure out how to fix these problems I've spent days with objdump, gdb, etc. and still can't fix this. I have figured out that the clang compiler can't cast anything so I can't figure out how to print something to the screen. I have no idea what the gnu assembler error is.

Here's my linker script

Code: Select all

ENTRY("Start")				#We start running the code at the Start function
TARGET(elf32-i386)			#We are writing our code for the i386 and above processor family and the binary is in a elf 32 file format

SECTIONS
{
	#We're starting our code at the address 0x1000000
	. = 0x1000000;

	#We're aligning our code to the next page
	.text ALIGN(0x1000) :{
	     *(.text)
	}

	.data ALIGN(0x1000) :{
	      *(.data)
	}

	.rodata ALIGN(0x1000) :{
		*(.rodata)
	}

	.bss ALIGN(0x1000) :{
	     *(.COMMON)
	     *(.bss)
	}
}
Although I don't think the linker script is the problem

Here's my entry.s file which is loaded from grub/other bootloader

Code: Select all

.global Start					#Make this readable from the linker
.extern main					#Find the main functions

#Multiboot Header Information
.align 4
        .set MAGIC, 0x1BADB002
        .set FLAGS, 3
        .set CHECKSUM, -(MAGIC + FLAGS)

#This is the first executed code
Start:
	cli
	mov $(StackEnd + StackSize), %esp	#Setup the stack

	cmp $0x2BADB002, %eax
	jne end
	
	push %ebx				#Pass the multiboot address pointer
	call main				#Call the main function

#This halts the system
end:
	cli
	hlt

.set  StackSize, 0x1000				#The stack is going to be 4096 bytes
.comm StackEnd, StackSize			#Reserve 4096 bytes for the stack
Here's my main.c code

Code: Select all

void main(int Multiboot)
{
  char *vidmem;
  vidmem=0xB8000;	//Video memory is at the address 0xB8000
  *vidmem='a';		//Print A to the scree
  vidmem++;
  *vidmem=0xF0;		//White on black
}
Lastly Here's my build.sh file

Code: Select all

assemble="as" #We're using the GNU Assembler
x86_options="-c" #We're
compile="clang"
clang_options="-ffreestanding -I../Include -nostdinc -c"
link="ld"
ld_options="-A i386 -T link.ld -o kernel.bin *.o"
mount_location="/media/floppy1"

echo "[Starting...]"

echo "Making OS virtual floppy image"
cp boot/image/os++.img Build

cd Core

echo "Creating kernel binary"
#Assemble code to x86
$assemble $x86_options entry.s entry.o

#Compile code to x86 elf format
$compile $clang_options main.c

#Link to x86 elf format
$link $ld_options

cd ..

echo "Copying object files"
cp Core/*.o Build/object
rm Core/*.o

echo "Copying build to build directory"
cp Core/kernel.bin Build
rm Core/kernel.bin

echo "Creating temporary mounting folder"
sudo mkdir /media/floppy1

echo "Mounting OS virtual floppy image"
sudo mount -o loop Build/os++.img $mount_location

echo "Copying build files to OS virtual floppy image"
cp Build/kernel.bin /media/floppy1

echo "Unmounting OS virtual floppy image"
cd Build
umount /media/floppy1
cd ..

echo "Removing Mounting Folder"
sudo rm -r /media/floppy1

echo "[Done]"
Thanks for your help,
Jackson
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: GAS end of file not at end of line and Clang casting

Post by JamesM »

Wow dude, this is a serious WTF.

Please read beginner mistakes - they apply to you. Know your toolchain and language well before coming here.

To actually answer your problem: For the assembler problem, you're not passing -o.

Code: Select all

$assemble $x86_options entry.s entry.o
Should be

Code: Select all

$assemble $x86_options entry.s -o entry.o
Secondly, in order to cast between an integer and a pointer you need to actually use a cast.

Code: Select all

vidmem = (char*)0xB8000
Please go away and learn the basics with something less taxing.

Cheers,

James
sounds
Member
Member
Posts: 112
Joined: Sat Feb 04, 2012 5:03 pm

Re: GAS end of file not at end of line and Clang casting

Post by sounds »

Kudos, however, for posting a complete set of files.

You'll get a lot of criticism here (but some good tips too).
Post Reply