Is it me, or is it GNU?

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
xhelloworld
Posts: 5
Joined: Thu Feb 05, 2009 5:25 pm
Location: Sweden

Is it me, or is it GNU?

Post by xhelloworld »

Hello everybody!

This is my first post, so please be gentle with me :wink: . I guess it's a nice gesture to briefly introduce myself, since I hope I will spend a lot of time here in the future. I'm not very good with forums.

My name is Jacob and I come from the, hopefully, well known country Sweden. I study games programming and would like to consider myself as a fairly good programmer. I have an arguably good insight in many fields of technology, though I have a quite limited knowledge in hardware. That is why I've started a little hobby kernel/OS project(the quantity, and quality, of the information here is tremendously great).

I will now try and get to the point.

I'm using Cygwin in windows 7 and everything is compiled, assembled and linked using makefiles, gcc, ar, ld, and nasm.

I've gone through the process of making a cross-compiler. I've made a good(good enough for me anyway) directory structure and followed Bran's kernel tutorial loosely, but I've done things my own way. I've not finished the hole tutorial, but the problem isn't the programming it's the linking.

I'm using a recursive makefile structure, and found the concept of archiving my sub-directories into dirname.a files easier to handle in lower-level makefiles(I don't yet know if this is a good strategy). I don't know if my problem is cygwin, gnu or me-being-better-at-cpp-than-c.

I'm very sorry for this very bad and long description of my problem. I think it is because of my bad English :wink:

/********THE PROBLEM IS DOWN BELOW IF YOU ARE ALREADY BORED TO DEATH*******/

I have a file src/drivers/char/screen.c which defines and implements the puts(const char*) function and the init_screen_d() function, amongst others. In my main I try to call those functions. screen.c has a header file which declares those functions with the keyword extern.

When I try to link all my made object files and my single archive i get this error:

Code: Select all

main.c:(.text+0x1f): undefined reference to 'puts'
At first i thought it was some sort of a symbol problem because of the composed archive(I used the $(TARGET)-ar archiver to archive the screen.o, for future ease of use). But turning the --verbose mode on the linker it said it read both the archive and the containing .o file correctly. The archive is listed last when linking, and I've also tried the flags --start-group archive --end-group, thinking it still wasn't reading it correctly. This produces another error:

Code: Select all

drivers/char/char.a: could not read symbols: Bad value
The strangest part is that the function init_screen_d() links perfectly fine(commenting out puts()), even though they are declared and implemented similarly. I've of course checked endlessly for typos.

Running ld with the flag --trace-symbol=puts, ld says that it finds the declarations and the definition.

What could be wrong/is wrong? Your help will force my endless gratitude!
js
Member
Member
Posts: 38
Joined: Thu Feb 26, 2009 1:45 am

Re: Is it me, or is it GNU?

Post by js »

Quick checks you could try :
  • Is main.c #include-ing the header file containing the puts declaration ?
  • Aren't any outdated files left over by make ? => delete *.o and *.a everywhere.
  • Try commenting out the whole body of puts, or even simply duplicating your init_screen_d() and calling it puts, and see if it links correctly (ie. is it a problem with the function's instruction, or with the declaration itself ?)
  • Try objdump with various flags to see what your *.o files contain (objdump -s file.o gives you an assembly dump of the file, and you can see the different functions, so you can check it contains the right code).
  • If you have access to a "real" unix machine, try compiling with the standard gnu toolchain to see if it's specific to cygwin
  • Is there a potential conflict with the standard, already existing puts ? (try gcc -fno-builtin -fno-stdlib -f... (can't remember the names but they are listed somewhere on the wiki)
  • Is your function declared "inline" or whatever (inline would likely not export the code for re-use unless you have specific gcc options) ?
  • Try reducing your case to the just two files main.c, puts.c, plus a makefile, and three functions puts, init_screen and main(), and dump the code here (as short as possible).
  • Try to avoid recursive make and make a flat make (or at least compile everything "by hand")
  • Try to compile all files at once (gcc -o my-os.bin main.c puts.c file-1.c file-2.c ...).
This might, or might not help / give you a hint about what's going on, so in any case, good luck and welcome :) .
xhelloworld
Posts: 5
Joined: Thu Feb 05, 2009 5:25 pm
Location: Sweden

Re: Is it me, or is it GNU?

Post by xhelloworld »

Man, this is a time consuming task...
Okey, here goes nothing.

[*] Is main.c #include-ing the header file containing the puts declaration ?
Of course :wink:. #include <pkernl/screen.h> in both main.c and screen.c
[*] Aren't any outdated files left over by make ? => delete *.o and *.a everywhere.
Yes, every time I build.
[*] Try commenting out the whole body of puts, or even simply duplicating your init_screen_d() and calling it puts, and see if it links correctly (ie. is it a problem with the function's instruction, or with the declaration itself ?)
Tried them all; Empty, the same as init.. I've changed now even switched permanently to another name 'putstr' so that it couldn't confuse it with some std stuff(it shouldn't be possible, but who knows)
[*] Try objdump with various flags to see what your *.o files contain (objdump -s file.o gives you an assembly dump of the file, and you can see the different functions, so you can check it contains the right code).
Yes, objdump confirms that the function is where and what it should be(tried diffrent implementations; empty, what it should be, e.t.c)
[*] If you have access to a "real" unix machine, try compiling with the standard gnu toolchain to see if it's specific to cygwin
I mean I could get access to a real machine, but that option would take too long right now. (I know, I promise that when I get the money I will buy a computer that contains only *nix. Game programming is in the way of me doing *nix instead of Win right now)
[*] Is there a potential conflict with the standard, already existing puts ? (try gcc -fno-builtin -fno-stdlib -f... (can't remember the names but they are listed somewhere on the wiki)
As I said earlier, I have tried avoiding std syntaxes and standards(something I don't want to do though). The commandline options should be what they are. But just for clarity, here they are:

Code: Select all

i586-elf-gcc -Wall -O -fstrength-reduce -fomit-frame-pointer -finline-functions -nostdinc -nodefaultlibs -fno-builtin -I $(INCLUDEDIR)
[*] Is your function declared "inline" or whatever (inline would likely not export the code for re-use unless you have specific gcc options) ?
Nope, not this one. I have others that do, but they should work. Nothing of inline-content is called in puts/putstr either(right now).
[*] Try reducing your case to the just two files main.c, puts.c, plus a makefile, and three functions puts, init_screen and main(), and dump the code here (as short as possible).
include/pkernl/screen.h:

Code: Select all

#ifndef _PKERNL_SCREEN_H
#define _PKERNL_SCREEN_H

#define BLACK     0
#define BLUE      1
#define GREEN     2
#define CYAN      3
#define RED       4
#define MAGENTA   5
#define BROWN 	  6
#define LGREY 	  7
#define DGREY 	  8
#define LBLUE	  9
#define LGREEN   10
#define LCYAN    11
#define LRED     12
#define LMAGENTA 13
#define LBROWN	 14
#define WHITE	 15

extern void cls();
extern void putch(char c);
extern void putstr(const char *str);
extern void settextcolor(unsigned char forecolor, unsigned char backcolor);
extern void init_screen_d();

#endif //_PKERNL_SCREEN_H
src/main.c:

Code: Select all

#include <pkernl/kernel.h>
#include <pkernl/types.h>

#include <pkernl/screen.h>

void main()
{
    //Initialization
    init_screen_d();
    putch('a');

    for (;;);
}
src/drivers/char/screen.c: (GO TO THE BOTTOM)

Code: Select all

#include <pkernl/screen.h>
#include <pkernl/string.h>
#include <asm/io.h>
/* These define our textpointer, our background and foreground
*  colors (attributes), and x and y cursor coordinates */
short *textmemptr;
int attrib = 0x0F;
int csr_x = 0, csr_y = 0;
/* Scrolls the screen */
void scroll(void)
{
    unsigned blank, temp;
    /* A blank is defined as a space... we need to give it
    *  backcolor too */
    blank = 0x20 | (attrib << 8);
    /* Row 25 is the end, this means we need to scroll up */
    if(csr_y >= 25)
    {
        /* Move the current text chunk that makes up the screen
        *  back in the buffer by a line */
        temp = csr_y - 25 + 1;
        memcpy (textmemptr, textmemptr + temp * 80, (25 - temp) * 80 * 2);
        /* Finally, we set the chunk of memory that occupies
        *  the last line of text to our 'blank' character */
        memsetw (textmemptr + (25 - temp) * 80, blank, 80);
        csr_y = 25 - 1;
    }
}
/* Updates the hardware cursor: the little blinking line
*  on the screen under the last character pressed! */
void move_csr(void)
{
    unsigned temp;
    /* The equation for finding the index in a linear
    *  chunk of memory can be represented by:
    *  Index = [(y * width) + x] */
    temp = csr_y * 80 + csr_x;
    /* This sends a command to indicies 14 and 15 in the
    *  CRT Control Register of the VGA controller. These
    *  are the high and low bytes of the index that show
    *  where the hardware cursor is to be 'blinking'. To
    *  learn more, look up some VGA specific
    *  programming documents. A great start to graphics:
    *  http://www.brackeen.com/home/vga */
    outportb(0x3D4, 14);
    outportb(0x3D5, temp >> 8);
    outportb(0x3D4, 15);
    outportb(0x3D5, temp);
}
/* Sets the forecolor and backcolor that we will use */
void settextcolor(unsigned char forecolor, unsigned char backcolor)
{
    /* Top 4 bytes are the background, bottom 4 bytes
    *  are the foreground color */
    attrib = (backcolor << 4) | (forecolor & 0x0F);
}
/* Clears the screen */
void cls()
{
    unsigned blank;
    int i;
    /* Again, we need the 'short' that will be used to
    *  represent a space with color */
    blank = 0x20 | (attrib << 8);
    /* Sets the entire screen to spaces in our current
    *  color */
    for(i = 0; i < 25; i++)
        memsetw (textmemptr + i * 80, blank, 80);
    /* Update out virtual cursor, and then move the
    *  hardware cursor */
    csr_x = 0;
    csr_y = 0;
    move_csr();
}
/* Puts a single character on the screen */
void putch(char c)
{
    short *where;
    unsigned att = attrib << 8;
    /* Handle a backspace, by moving the cursor back one space */
    if(c == 0x08)
        if(csr_x != 0) 
			csr_x--;
    /* Handles a tab by incrementing the cursor's x, but only
    *  to a point that will make it divisible by 8 */
    else if(c == 0x09)
        csr_x = (csr_x + 8) & ~(8 - 1);
    /* Handles a 'Carriage Return', which simply brings the
    *  cursor back to the margin */
    else if(c == '\r')
        csr_x = 0;
    /* We handle our newlines the way DOS and the BIOS do: we
    *  treat it as if a 'CR' was also there, so we bring the
    *  cursor to the margin and we increment the 'y' value */
    else if(c == '\n')
	{
        csr_x = 0;
        csr_y++;
    }
    /* Any character greater than and including a space, is a
    *  printable character. The equation for finding the index
    *  in a linear chunk of memory can be represented by:
    *  Index = [(y * width) + x] */
    else if(c >= ' ')
    {
        where = textmemptr + (csr_y * 80 + csr_x);
        *where = c | att;	/* Character AND attributes: color */
        csr_x++;
    }
    /* If the cursor has reached the edge of the screen's width, we
    *  insert a new line in there */
    if(csr_x >= 80)
    {
        csr_x = 0;
        csr_y++;
    }
    /* Scroll the screen if needed, and finally move the cursor */
    scroll();
    move_csr();
}
/* Uses the above routine to output a string... */
void putstr(const char *str)
{
    /*int i;
    for (i = 0; i < strlen(str); i++)
        putch(str[i]);*/
}
/* Sets our text-mode VGA pointer, then clears the screen for us */
void init_screen_d(void)
{
    textmemptr = (short*)0xB8000;
    cls();
}
[*] Try to avoid recursive make and make a flat make (or at least compile everything "by hand")
There shouldn't be anything wrong with my few makefiles; I've spent hours staring and polishing them, very thoroughly. I've
tried to do manual builds aswell. No result.
[*] Try to compile all files at once (gcc -o my-os.bin main.c puts.c file-1.c file-2.c ...).
This one is the only one I've tried but failed. That is because I get some error about crx0.o or something in the linker stage, I've read that it should be a standard library thing. I've spent minor time looking around for a "quick fix", turning stdlib of in the ld via gcc. But with no success. I, of course, use all the flags in gcc that should turn of stdlibs, which in turn should turn off ld's link to stdlibs. (Note my sloppy way of describing "standard libraries/predefined" as stdlibs).

If you do know what's causing this/what to do about it I'd be glad to test it out!
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Is it me, or is it GNU?

Post by gerryg400 »

In your linker command line (of your makefile), do you use ld or gcc. gcc is better (it runs ld and collect2 as well). Sometimes it gives different or better error messages that may help.
If a trainstation is where trains stop, what is a workstation ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Is it me, or is it GNU?

Post by gerryg400 »

BTW, are you outputting a binary file or an ELF file. Some people have reported odd link problems when building a flat binary on Cygwin.
If a trainstation is where trains stop, what is a workstation ?
xhelloworld
Posts: 5
Joined: Thu Feb 05, 2009 5:25 pm
Location: Sweden

Re: Is it me, or is it GNU?

Post by xhelloworld »

gerryg400 wrote:In your linker command line (of your makefile), do you use ld or gcc. gcc is better (it runs ld and collect2 as well). Sometimes it gives different or better error messages that may help.
After all object- and archive files are created ld is invoked. You may be right, it may work if I use gcc instead.

As I mentioned, badly explained, earlier I tried using gcc manually to link but the linker(ld) complains about some 'cx0.o', or something like that. I briefly tried finding info on how to correct this(the linker tries to link in some stdlib right?) but I didn't find anything about that problem, that didn't have to do with compiling libc itself.

If it is to any help I'll post the cmd and the error.
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Is it me, or is it GNU?

Post by gerryg400 »

If you are building a binary file, remove that from the linker script. A few people on the list have had a very similar problem to this. I suspect a bug in the Cygwin port of binutils w.r.t. flat binary output file.
If a trainstation is where trains stop, what is a workstation ?
xhelloworld
Posts: 5
Joined: Thu Feb 05, 2009 5:25 pm
Location: Sweden

Re: Is it me, or is it GNU?

Post by xhelloworld »

gerryg400 wrote:If you are building a binary file, remove that from the linker script. A few people on the list have had a very similar problem to this. I suspect a bug in the Cygwin port of binutils w.r.t. flat binary output file.
Yes, seemed i did output in flat binary. Do I change the linkerscript to:

OUTPUT_FORMAT("elf")
... ?
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Is it me, or is it GNU?

Post by gerryg400 »

Just remove that line altogether.
If a trainstation is where trains stop, what is a workstation ?
xhelloworld
Posts: 5
Joined: Thu Feb 05, 2009 5:25 pm
Location: Sweden

Re: Is it me, or is it GNU?

Post by xhelloworld »

gerryg400 wrote:Just remove that line altogether.
You are my savior. It seems to work...
Post Reply