C string doesn't work when cross compile c kernel.
Posted: Thu Aug 15, 2019 12:48 am
Issue: strings defined as
are not readable (defining them as const char greeting_msg[] solve problem).
Full source code:
https://github.com/YuRaZaKa/AltOS/tree/master
C file with issue:
Makefile
I am using latest cross compiled gcc (9.1.0) and binutils (2.32)
Code: Select all
const char* greeting_msg = "Hello, world!";
Full source code:
https://github.com/YuRaZaKa/AltOS/tree/master
C file with issue:
Code: Select all
#include <stddef.h>
#include <stdint.h>
uint8_t* video;
void cls(){
video = 0xb8000;
for(size_t i = 0; i < 25*80; ++i){
*(video++) = ' ';
*(video++) = 0x1F;
}
video = 0xb8000;
}
void write(const char* data){
for(size_t i = 0; data[i] != '\0'; ++i){
*(video++) = data[i];
*(video++) = 0x1F;
}
}
void kmain(void){
cls();
const char* greeting_msg = "Hello, world!";
write(greeting_msg);
}
Code: Select all
osname = AltOS
init:
rm -rf $(osname).iso
rm -rf obj
rm -rf bin
rm -rf iso
mkdir obj
mkdir bin
mkdir iso
mkdir iso/boot
mkdir iso/boot/grub
compile:
x86_64-elf-gcc src/c/kmain.c -o obj/kmain.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -nostdlib
assembly:
nasm -f elf64 src/asm/multiboot_header.asm -o obj/multiboot_header.o
nasm -f elf64 src/asm/boot.asm -o obj/boot.o
nasm -f elf64 src/asm/long.asm -o obj/long.o
link:
x86_64-elf-ld -n -o bin/kernel.bin -T src/ld/linker.ld obj/kmain.o obj/multiboot_header.o obj/boot.o obj/long.o
grubprepare:
cp bin/kernel.bin iso/boot
echo 'set timeout=0' >> iso/boot/grub/grub.cfg
echo 'set default=0' >> iso/boot/grub/grub.cfg
echo 'menuentry "AltOS" {' >> iso/boot/grub/grub.cfg
echo ' multiboot2 /boot/kernel.bin' >> iso/boot/grub/grub.cfg
echo ' boot' >> iso/boot/grub/grub.cfg
echo '}' >> iso/boot/grub/grub.cfg
image:
grub-mkrescue -o $(osname).iso iso
vm:
qemu-system-x86_64 -cdrom $(osname).iso
run: init compile assembly link grubprepare image vm