Page 1 of 1

How to link without errors? [SOLVED, lmao so easy]

Posted: Sun Oct 09, 2016 4:45 am
by NunoLava1998
Original question i asked on SO: http://stackoverflow.com/questions/3991 ... -linker-ld
I am currently following the Bare Bones tutorial from OSDev and i have set up everything and compiled the kernel and bootloader into object files, and i have the .ld file. However, when i try to link the files and produce it into a bin using:

Code: Select all

i686-elf-gcc -T linker.ld -o myos.bin -ffreestanding -O2 -nostdlib boot.o kernel.o -lgcc
, i get this error:

"

Code: Select all

.../i686/bin/../lib/gcc/i686-elf/4.8.2/../../../../i686-elf/bin/ld.exe: cannot open linker script file linker.ld

collect2.exe: error: ld returned 1 exit status
"

I know what "ld returned 1 exit status" means (ld has gotten a error, not a error itself, just indicating errors.). The other solutions i have tried either give more errors or do absolutely nothing. Adding -T does nothing. I'm using Windows 10 and have the .exe's and etc. from a source which i don't have the link to, and i'm using GCC. I also put 4 dll's for gcc to work without errors. I have already compiled the assembly and C into object files, but nothing. I removed every part that has to do with GRUB/Multiboot except protected mode (possibly) and 32 bit code, and there are no errors in the code. I've never seen a solution that involves "cannot open linker script file linker.ld", and linking by default either literally ignores every piece of assembly code beacuse it's not "machine code" (it is, normally ignores /001 or /000 or /003 or &) or gives the same error (possibly with a few more errors).

Re: How to link without errors?

Posted: Sun Oct 09, 2016 4:55 am
by iansjack
The error message seems quite clear. Either linker.ld does not exist in the current working directory or its permissions forbid you access.

Re: How to link without errors?

Posted: Sun Oct 09, 2016 4:59 am
by NunoLava1998
iansjack wrote:The error message seems quite clear. Either linker.ld does not exist in the current working directory or its permissions forbid you access.
So where can i get linker.ld then?

Re: How to link without errors?

Posted: Sun Oct 09, 2016 5:03 am
by iansjack

Re: How to link without errors?

Posted: Sun Oct 09, 2016 5:08 am
by NunoLava1998
iansjack wrote:http://wiki.osdev.org/Bare_Bones#Linking_the_Kernel
Oh, just realized i didn't name my linker "linker.ld". #-o :lol:

Re: How to link without errors?

Posted: Sun Oct 09, 2016 5:13 am
by NunoLava1998
And now it's giving a syntax error on linker.ld saying the code between the triple parenthesis is incorrect (the real code doesn't include the paranthesis and i don't want the OS to be started with GRUB) in this code is incorrect:

Code: Select all

ENTRY(_start)

SECTIONS
{

	. = 1M;



	.rodata BLOCK(4K) : ALIGN(4K)
	{
		*(.rodata)
	}


	.data BLOCK(4K) : ALIGN(4K)
	{
		*(.data)
	}


	.bss BLOCK(4K) : ALIGN(4K)
	{
		*(COMMON)
		*(.bss)
	}


((( } )))

Re: How to link without errors?

Posted: Sun Oct 09, 2016 5:17 am
by NunoLava1998
nvm, got it working. it was the return 0; i had put, lol now it's linked and i can make a quick bat file to make the linked .bin finally

Re: How to link without errors?

Posted: Sun Oct 09, 2016 5:20 am
by iansjack
How are you going to boot the kernel if you don't use GRUB?

Re: How to link without errors?

Posted: Sun Oct 09, 2016 6:22 am
by NunoLava1998
@iansjack a 512 byte bootloader which should load sector 2, which contains the ASM file to load the C file and it's headers and link.ld. i have dd to place the code into sector 2 (it can extend beyond sector 2, it just starts at sector 2).

Re: How to link without errors?

Posted: Wed Oct 12, 2016 10:41 am
by Schol-R-LEA
NunoLava1998 wrote:@iansjack a 512 byte bootloader which should load sector 2, which contains the ASM file to load the C file and it's headers and link.ld. i have dd to place the code into sector 2 (it can extend beyond sector 2, it just starts at sector 2).
Is there a compelling reason why you want to write your own boot loader?

(NB: just saying "I want to learn how to write one" is enough to count, though I would caution against then using that boot loader as your OS loader unless you are willing to spend more time on the boot loader than on the rest of the OS and dealing with an excessive amount of frustration along the way.)

The boot loader is a very small part of the system, takes a disproportionate amount of development time for its significance, and is very difficult to get right to a production scale (even for a hobbyist level of 'production'), which is why we usually recommend going with an existing boot loader for your actual OS.

There are reasons to write your own boot loader, but the are usually not very good ones. While I am intending to write one myself, it is because my own system is meant to use a very anomalous document system that doesn't play well with conventional file structures, and because I think I might be able to improve on GRUB (unlikely, but why not try?). In the end, though, I realize I am driven by egotism rather than rational goals, and have accepted that.

The point is that you need to think of both your motivations and your goals before committing to a substantial project that is, in the final analysis, likely to be a detour rather than a direct route. Will writing your own boot loader teach you anything significant? Possibly; it can serve as a decent warm-up for more complex problems. Does it further your goal of writing an operating system? Probably not, unless you are doing something very exotic with the file system or the boot sequence. I suggest you give that some thought before going further.

And set up a repo on somewhere like GitHub or Cloudforge. Seriously, you will come to regret it if you don't, and it would make showing us your code problems a loooot easier.