OSDev.org https://forum.osdev.org/ |
|
C string doesn't work when cross compile c kernel. https://forum.osdev.org/viewtopic.php?f=1&t=33829 |
Page 1 of 1 |
Author: | yuriyzam [ Thu Aug 15, 2019 12:48 am ] |
Post subject: | C string doesn't work when cross compile c kernel. |
Issue: strings defined as Code: const char* greeting_msg = "Hello, world!"; 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: Code: #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); } Makefile Code: 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 I am using latest cross compiled gcc (9.1.0) and binutils (2.32) |
Author: | MollenOS [ Thu Aug 15, 2019 2:52 am ] |
Post subject: | Re: C string doesn't work when cross compile c kernel. |
What happens when the code runs? |
Author: | yuriyzam [ Thu Aug 15, 2019 4:22 am ] |
Post subject: | Re: C string doesn't work when cross compile c kernel. |
Quote: What happens when the code runs? If const char[] used it outputs "Hello, world!" as it should. Otherwise, nothing will be displayed (only blue background will). Before that, it checks multiboot and set long mode. Maybe the problems are in GDT? |
Author: | iansjack [ Thu Aug 15, 2019 4:27 am ] |
Post subject: | Re: C string doesn't work when cross compile c kernel. |
The difference between the two is where the string is stored. const char * - the string is stored in the .rodata section const char[] - the string is stored on the stack This should work, except for the fact that you are giving your linker conflicting information. When you compile kmain.c you link it at the same time. So kmain.o is an executable, not a simple object file. The linker assumed that the file is loaded at 0x400000 (the default), with the .rodata section at 0x400151. You then link the executable again, this time telling it that the program is loaded at 0x100000, with the .rodata section at 0x1001f0. When the program runs it thinks the string is at 0x400151, and prints the empty data there. The answer is that you need to use the -c switch when compiling kmain.c to produce an object file rather than an executable. |
Author: | yuriyzam [ Thu Aug 15, 2019 5:21 am ] |
Post subject: | Re: C string doesn't work when cross compile c kernel. |
Thanks, it worked/ Btw, how I should solve this for multiply c files? Code: x86_64-elf-gcc -c src/c/kmain.c src/c/vgacolor.c -o obj/kmain.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra -nostdlib
x86_64-elf-gcc: fatal error: cannot specify -o with -c, -S or -E with multiple files compilation terminated. |
Author: | iansjack [ Thu Aug 15, 2019 5:41 am ] |
Post subject: | Re: C string doesn't work when cross compile c kernel. |
You compile each source file to an object file separately. |
Author: | Solar [ Thu Aug 15, 2019 8:03 am ] |
Post subject: | Re: C string doesn't work when cross compile c kernel. |
Perhaps check out the wiki on Makefiles. |
Page 1 of 1 | All times are UTC - 6 hours |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |