Bran's Kernel Development Tutorial (need help linking!)

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.
CompDever
Posts: 23
Joined: Sun Jul 22, 2007 12:21 pm

Bran's Kernel Development Tutorial (need help linking!)

Post by CompDever »

I was working through Bran's Kernel Development Tutorial, and as i was doing this i came to the point of putting text on the screen. So, following the tutorial i did what it said and had all the code in files that stuff. When i tried to compile and link the code it said:
Linker wrote: c:/djgpp/bin/ld.exe: section .data [00100400 -> 001013ff] overlaps section .text [00100000 -> 0010072f]
ostest.o(.text+0x2d): undefined reference to `main'
scrn.o(.text+0xef):scrn.c: undefined reference to `memcpy'
scrn.o(.text+0x108):scrn.c: undefined reference to `memsetw'
scrn.o(.text+0x136):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x147):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x156):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x16a):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x1b9):scrn.c: undefined reference to `memcpy'
scrn.o(.text+0x1d2):scrn.c: undefined reference to `memsetw'
scrn.o(.text+0x208):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x219):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x228):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x23c):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x276):scrn.c: undefined reference to `memsetw'
scrn.o(.text+0x2a8):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x2b7):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x2c6):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x2d5):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x30a):scrn.c: undefined reference to `strlen'
scrn.o(.text+0x378):scrn.c: undefined reference to `memsetw'
scrn.o(.text+0x3aa):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x3b9):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x3c8):scrn.c: undefined reference to `outportb'
scrn.o(.text+0x3d7):scrn.c: undefined reference to `outportb'
according to my linker call
C:\djgpp\bin\ld.exe link.ld -o main.o ostest.o scrn.o
main.o is the first object to be linked. When it gets to scrn.o it created the errors seen above. Can anyone tell me what i've done wrong? and btw, what does the linker do anyway (for this)?
Last edited by CompDever on Tue Jul 24, 2007 9:28 am, edited 1 time in total.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

It looks like you are trying to link data to the same location as code (as per the addresses in your error message). Could you post your linker script as that is likely to be the problem.

Cheers,
Adam
CompDever
Posts: 23
Joined: Sun Jul 22, 2007 12:21 pm

Post by CompDever »

Here it is:
Link.ld wrote: OUTPUT_FORMAT("binary")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
.text phys : AT(phys) {
code = .;
*(.text)
*(.rodata)
. = ALIGN(4096);
}
.data : AT(phys + (data - code))
{
data = .;
*(.data)
. = ALIGN(4096);
}
.bss : AT(phys + (bss - code))
{
bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .;
}
And what is the purpose of the linker in this project?
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Post by AJ »

I believe .rodata should be with .data rather than with .text. Alternatively, give .rodata a complete other section. Although I am not over confident with linker scripts, that may help. Somehow your section placement (the AT(phys + ...) is overlapping 2 of the sections.

The purpose of this linker script is to put data, code and bss at known locations in the binary. Because Bran's tutorial outputs a flat binary file, there is no format information and the entry point of the kernel must therefore be at the start of the executable image. It also places different sections on page aligned boundaries.

If you were to use an elf file, you may not need the linker script (although it is often useful anyway) as the information to relocate the executable file is contained in the elf program headers.

HTH,
Adam
CompDever
Posts: 23
Joined: Sun Jul 22, 2007 12:21 pm

Post by CompDever »

but, my linker call (C:\djgpp\bin\ld.exe link.ld -o main.o ostest.o scrn.o) has no ouput? and doesn't this (copy /b ostest.o+scrn.o+main.o ostest.img) do what the linker would do (that is in my batch file)? (and i don't know enough about linker scripts to reorganize one, so a completed version would be the best, if possible)
CompDever
Posts: 23
Joined: Sun Jul 22, 2007 12:21 pm

Post by CompDever »

umm, why is there a sudden stop in the replies to this should be simple thread? it has been hours since the last reply, whereas the entire first of the thread took place in less than an hour.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

CompDever wrote:umm, why is there a sudden stop in the replies to this should be simple thread? it has been hours since the last reply, whereas the entire first of the thread took place in less than an hour.
Wow, are you serious? :roll:

People take time out of their day to help people here, I don't think anyone will help you now though...

Why don't you trying fixing it yourself? 8)
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
User avatar
Kevin McGuire
Member
Member
Posts: 843
Joined: Tue Nov 09, 2004 12:00 am
Location: United States
Contact:

Post by Kevin McGuire »

Here is the answer encoded in my secret message format:
The undefined reference happens xifo+uif+dpnqjmfs+dbo+opu+gjoe+b symbol in the source file it is+dpnqjmjoha+ju+xjmm+uifo+fnju+uiese symbol into a table and add tpnf+sfmpdbujpo+fousjft+tp+uibu+the linker can later fill this iogpsnbujpo+jo-+Xifo+uif+mjolfs+fncounteres this object file prodvdfe+gspn+uif+tpvsdf+gjmf+ju+xjml search through all the object gjmft+zpv+hbwf+jua+boe+jg+ju+tujll can not find this symbol defiofe+tpnfxifsf+ju+xjmm+ejtqmbz+uie undefined reference message siodf+pcwjpvtmz+uif+sfgfsfodf+up+uhis something was not defined. Tijt+tpnfuijoh+bqqfbst+up+cf+b+gvnction called memcpy, outportb, nfntfua+tusmfoa+boe+joqpsuc-+Uif memcpy function is used to copy+nfnpsz+gspn+pof+qmbdf+up+bopuifr and is quite simple to write. Uif+pvuqpsuc+boe+joqpsuc+bsf+tnbll quick to write functions that+sfbe+ps+xsjuf+b+czuf+up+b+J(P+qort. The strlen is used to count+ipx+nboz+dibsbdufst+bsf+qsftfou in a null-terminated string. Thf+nfntfu+jt+vtfe+up+tfu+b+dfsubjn amount of memory at a specifiee+beesftt+up+uif+tqfdjgjfe+czuf+value - useful for clearing someuijoh+up+Afspt+mjlf+b+tusvduvsf+(struct in C/C++).

Code: Select all

	uint8_t cset[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
				'A', 'B', 'C', 'D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
				'1','2','3','4','5','6','7','8','9','0',' ', '+', '/', '(', ')', '.', '-', ','};

	for(uint32_t x = 0; msg[x] != 0; ++x)
	{
		if(x & 32)
		{	
			for(uint32_t y = 0; y < sizeof(cset); ++y)
			{
				if(cset[y] == msg[x])
				{
					msg[x] = y;
					if(msg[x] + 1 >= sizeof(cset))
					{
						msg[x] = 0;
					}else{
						++msg[x];
					}
					printf("%c", cset[msg[x]]);
					break;
				}	
			}
		}else{
			printf("%c", msg[x]);
		}
	}
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

@CompDever:

Piss off and solve it on your own. Seriously. I know this is a resource to help people but this is just taking the piss - 3 threads in 2 days created by you, for trivial problems. And I'm not saying they're trivial because I'm experienced in OS-dev, but because I can *read error messages instead of posting them immediately*

I also was not stupid enough to learn C++ for 2 weeks, expect to be fluent and embark on a hobby OS project, which just so happens to be the most difficult thing you can do (with the exception maybe of writing a compiler, which is what I essentially do for a living).

Take a step back. If you dont know what a linker is and why it's used, *you REALLY shouldn't be trying to make your own kernel*. REALLY.

Do some more simple low level stuff. Or high level for that matter. You're in windows, so I assume you're using Visual Studio as much as you can. That has a linker (it's an essential part of a compiler package). Look in the project options menu, it gives you switches etc to fiddle with.

What really pisses me off is that you are posting these questions when the answers to ALL of them are on the internet somewhere in easy reach. Yet you ask the question on a forum then flame when nobody's replied for hours?

You are what's wrong with the internet.

JamesM

EDIT:
and doesn't this (copy /b ostest.o+scrn.o+main.o ostest.img) do what the linker would do (that is in my batch file)?
*Tears hair out of head* http://en.wikipedia.org/wiki/Linker SEARCH WIKI/GOOGLE BEFORE YOU POST USELESS QUESTIONS! it is quite obvious you've done no research at all.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

CompDever wrote:but, my linker call (C:\djgpp\bin\ld.exe link.ld -o main.o ostest.o scrn.o) has no ouput? and doesn't this (copy /b ostest.o+scrn.o+main.o ostest.img) do what the linker would do (that is in my batch file)? (and i don't know enough about linker scripts to reorganize one, so a completed version would be the best, if possible)
Make sure you compile every source file (the tutorial is not a 'compile-as-you-go' sort of tutorial). Once you've done that every compiled object must be linked into the final executable.

The undefined references are caused by the lack of the object file being linked in containing 'memcpy', 'memsetw', 'outportb' (et al).
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

CompDever wrote:And what is the purpose of the linker in this project?
Wow. Exactly how long have you been programming to not know what a linker is for?

I programmed C/C++ for 2 years (Windows stuff mainly) until I finally decided to do OS development and even then it was still difficult.

I suggest writing some programs in GCC for your main OS (not your OS in development) and get used to the command line. Play around a bit and then try to understand what happens when you link a program.
User avatar
Alboin
Member
Member
Posts: 1466
Joined: Thu Jan 04, 2007 3:29 pm
Location: Noricum and Pannonia

Post by Alboin »

pcmattman wrote:I suggest writing some programs in GCC for your main OS (not your OS in development) and get used to the command line. Play around a bit and then try to understand what happens when you link a program.
Yeah. I think it's been said before that 1 or 2 large-ish projects are appropriate before any osdev is attempted.
C8H10N4O2 | #446691 | Trust the nodes.
User avatar
os64dev
Member
Member
Posts: 553
Joined: Sat Jan 27, 2007 3:21 pm
Location: Best, Netherlands

Post by os64dev »

@compdever
What do you expect from people here. They have there own projects and life te worry about instead of aiding a newbie kid with no patience (trust me i am saying this nicely). Most of us have several years of programming experience and did a lot of research/studying on programming. The fact that you don't know what a linker tells us that you didn't even bother to search for the answer and makes you a hopeless cause for OS developping as it is one of the hardest things in software developping. So i guess you have to learn a bit more about the subject and become a little older then 8... darn sarcastic again.
JamesM wrote:Piss off and solve it on your own. Seriously. I know this is a resource to help people but this is just taking the piss - 3 threads in 2 days created by you, for trivial problems. And I'm not saying they're trivial because I'm experienced in OS-dev, but because I can *read error messages instead of posting them immediately*
I looked also and they are very trivial :twisted: .
Author of COBOS
CompDever
Posts: 23
Joined: Sun Jul 22, 2007 12:21 pm

Post by CompDever »

@JamesM: Ok... i was not flaming but rather asking a question about the sudden inconsistency of responses and on aside note: that is flame, what you have written. I am now essentially fluent in c++, just because you can't learn that fast does not mean others cannot learn that fast. I do know what a linker does. That is not the question. The question was what does a linker do here.
Wikipedia [http://en.wikipedia.org/wiki/Linker] wrote: In computer science, a linker or link editor is a program that takes one or more objects generated by compilers and assembles them into a single executable program.
but here, we are not creating an executable as it refers to in that context. We are creating a flat binary. The question comes from the fact that my linker call [C:\djgpp\bin\ld.exe link.ld -o main.o ostest.o scrn.o] has no apparent output, and that call came from Bran's Kernel Dev Tutorial. If you do not like the fact that somebody is so intrigued by OS Development that he will try to learn it as quickly as possible; to the point of asking more experienced people for explanations of the milestone's encountered; which in turn will enable me to fully understand how to solve that same milestone later; then you [JamesM] should quit coming to OSDev forums. When one speaks of flaming, he should be careful to not flame; lest he become a flamer himself. I was not flaming. I am simply curious by nature.

@Brynet-Inc: It is difficult for one to find the answer if he does not understand the question.

@Kevin McGuire: lol, that helps [not really!] :D

@Everyone: read my post towards JamesM; i do understand what a linker is, just not its purpose in this context. Additionally if my linker has no output, then what use does it serve? I am not trying to be mean, or any of that; even if sounds like it. That is what JamesM is doing, flaming an OSDev Noob; How professional is that?
I learned all other programming through research, this is too advanced for just research.

@pcmattman: i would do that, but i looked at the linker options and there were a lot of options and i did not care to read all of those options to find the relevant ones, since i would practically have to take notes on the usage of ld.exe, which i may do sometime, but i don't feel the neccessity of pouring over the linker for a couple of hours right now.

@Alboin: I believe that i have already done '1 or 2 large-ish projects' in MSVC++

@os64dev: I do not understand why there are error messages at all! i have everything that i should have readily there. I figure it must be a command line option, that is it. (and about expecting from people) I just know that there is not enough information out there to counter asking a well-experinced person for an answer to a question. There answer is almost always better since it teaches me about what i am doing, not what somebody else has done, which i have to figure how relates to me.

@everyone again: i have attached, my OS in progress, that follows Bran's Tutorial (discussed earlier). I could not include nasm.exe, since it was too big but everthing else is in there and read the txt file with instructions.
Attachments
ostest.zip
the os, that is being built
(6.04 KiB) Downloaded 112 times
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

OK, first of all, yes I know I flamed, and I'm not apologising. Because a simple google could still have told you the answer.
but here, we are not creating an executable as it refers to in that context. We are creating a flat binary.
Yes, you are creating an executable. The file format doesn't matter. You are still creating an exectutable file, with .text, .data, and .bss sections (minimum).

You say you know what a linker is. Well, that may be, but you obviously don't know the basic intrinsics of how a linker works. I'm not talking in-depth here, I'm talking the basic basic stuff you'd learn on any wiki page or tutorial.

The bit I'm referring to is this:
compile ostest.bat wrote: :DoPartCopy
copy /b ostest.o+scrn.o+main.o ostest.img
GOTO exita
You are just pasting all the object files together. That will NEVER create a valid executable, thats what a linker is for. Imagine a symbol is defined in ostest.c, and used in scrn.c. When compiled, scrn.o will contain a placeholder stating that symbol's name. ostest.o will contain the definition of that symbol. The linker needs to replace the symbol placeholder in scrn.o with the actual absolute address of the symbol in ostest.o, after pasting them all together (in a more intelligent fashion). Then it needs to add static data to the .data segment, and to (in a flat binary) write a certain number of zeroes to the .bss segment so it's ready for use.
my linker call [C:\djgpp\bin\ld.exe link.ld -o main.o ostest.o scrn.o] has no apparent output,
Are you sure? a quick google will get you the man page for ld (which had you been working in a unix (even emulated) environment you could have gotten by typing 'man ld'), and also a 'ld.exe --help' (or 'ld.exe /help' - not hot on the syntax for M$) would have told you that the default output file is called 'a.out'. And that doesn't get created until the linker finishes successfully (which it is not, here).
I am now essentially fluent in c++, just because you can't learn that fast does not mean others cannot learn that fast.
Don't talk such bullshit. You can take a fast-track course in French and be 'essentially fluent' in a month - it takes half a lifetime to be fully fluent.
The same is true (to an extent) with languages like C/C++. Because they live so low down in the chain of languages (most other languages' compilers/interpreters are written in C) there are complex features that tend to take a while to understand. C++ also has many 'eccentricities' - places where the compiler does something other than what you may expect, that take a while to learn.

Don't get me wrong, I'm not saying you need to have studied C++ all your life to code an OS. Not at all, many people on these forums only picked up C a short while before they started coding an OS. But I can bet you they knew what a linker was before they started! AND they CERTAINLY didnt post 3 threads in 2 days about standard C/linker errors, then complain about the lack of attention he was getting! You seem to give up much too easily and try and pass the buck on to others. The errors you are reporting are *standard error messages*, nothing special. I'll take a look at your code and post back in a bit as to what I think may be the problem (I dont ahve a windows toolchain so I can't really test it out).

If you 'became almost fluent in c++ in 2 weeks' that shows a lot of commitment, however I feel inclined to doubt that statement based on the ease at which you give up.

JamesM
Post Reply