Page 1 of 1

Annoying ld linking problem

Posted: Wed Mar 14, 2012 1:47 pm
by drunkenfox
So, I have successfully compiled an object file out of my c++ source

Code: Select all

#include "k_defines.h"

inline void outportb(unsigned int port, unsigned char value);

unsigned int panic(char *message);
void k_main();

void k_clear_screen();
unsigned int k_printf(char *message, unsigned int line);
void update_cursor(int row, int col);

void k_main()
{
	k_clear_screen();

	k_printf("Starting P-DOS...",0);
	update_cursor(1,0);
	k_printf("Nopony else is in here. Looks like I'm forever aLUNA. (How punny of me *induberdebly*)",3);
	update_cursor(5,0);
};

unsigned int k_printf(char *message, unsigned int line)
{
	char *vram = (char *) 0xb8000;
	unsigned int i=0;

	i=(line*80*2);

	while(*message!=0)
	{
		if(*message==0x2f)
		{
			*message++;
			if(*message==0x63)
			{
				line++;
				i=(line*80*2);
				*message++;
				if(*message==0)
				{
					return(1);
				};
			};
		};
		vram[i]=*message;
		*message++;
		i++;
		vram[i]=0x07;
		i++;
	};

	return(1);
};

void k_clear_screen()
{
	char *vram = (char *) 0xb8000;
	unsigned int i=0;
	while(i<(80*2*25))
	{
		vram[i]=' ';
		i++;
		vram[i]=WHITE_TXT;
		i++;
	};
};

inline void outportb(unsigned int port, unsigned char value)
{
	asm volatile ("outb %%al, %%dx"::"d" (port), "a" (value));
};

void update_cursor(int row, int col)
{
	USHORT position=(row*80)+col;

	outportb(0x3d4, 0x0f);
	outportb(0x3d5, (UCHAR)(position&0xff));

	outportb(0x3d4, 0x0e);
	outportb(0x3d5, (UCHAR)((position<<8)&0xff));
};

unsigned int panic(char *message)
{
	char *vram = (char *) 0xb8000;
	unsigned int i=0;
	unsigned int line=0;

	i=(line*80*2);

	while(*message!=0)
	{
		if(*message==0x2f)
		{
			*message++;
			if(*message==0x63)
			{
				line++;
				i=(line*80*2);
				*message++;
				if(*message==0)
				{
					return(1);
				};
			};
		};
		vram[i]=*message;
		*message++;
		i++;
		vram[i]=0x9; //use blue text
		i++;
	};

	return(1);
};

k_defines.h

Code: Select all

#define WHITE_TXT 0x7
#define BLUE_TXT 0x09

#define UCHAR unsigned char
#define USHORT unsigned short


kernel.ld

Code: Select all

OUTPUT_FORMAT("binary")
ENTRY(k_main)
SECTIONS
{
  .text  0x005000 : {
    code = .; _code = .; __code = .;
    *(.text)
    . = ALIGN(4096);
  }
  .data  : {
    data = .; _data = .; __data = .;
    *(.data)
    . = ALIGN(4096);
  }
  .bss  :
  {
    bss = .; _bss = .; __bss = .;
    *(.bss)
    . = ALIGN(4096);
  }
  end = .; _end = .; __end = .;
}


build script (batch)

Code: Select all

echo off
gcc-3 -c pony.cpp -o pony.o
ld -T kernel.ld -o PONY.SYS pony.o
copy PONY.SYS A:\PONY.SYS

pause
But I just get an error on the linker:

Code: Select all

ld: cannot perform PE operation on a non PE file 'PONY.SYS'
If anyone could help, it would be appreciated.

Re: Annoying ld linking problem

Posted: Wed Mar 14, 2012 1:53 pm
by bluemoon
ponyboy wrote:ld: cannot perform PE operation on a non PE file 'PONY.SYS'
Guess what? There is a wiki page for that. Cannot_perform_PE_operations_on_non-PE_file

Re: Annoying ld linking problem

Posted: Wed Mar 14, 2012 5:08 pm
by drunkenfox
I'm trying get a flat binary. I don't want a PE. Btw, I have win xp and cygwin for the compiler.

Re: Annoying ld linking problem

Posted: Wed Mar 14, 2012 5:51 pm
by brain
Are you REALLY sure you need a flat binary? Flat binary does not have facilities for relocation, symbol tables, and lots of other useful stuff. If you REALLY do need flat binary try the --oformat parameter to ld.

Re: Annoying ld linking problem

Posted: Wed Mar 14, 2012 6:16 pm
by drunkenfox
It's going to be the real mode os loader after loaded at 0x500 by broken thorn's bootsector.

Re: Annoying ld linking problem

Posted: Wed Mar 14, 2012 10:50 pm
by Rudster816
ponyboy wrote:It's going to be the real mode os loader after loaded at 0x500 by broken thorn's bootsector.
You can't use GCC to produce 16 bit machine code. You're pretty much stuck with assembly for real mode unless you want to try and learn how to use the OpenWatcom compiler.

Re: Annoying ld linking problem

Posted: Thu Mar 15, 2012 12:36 am
by Solar
1) What bluemoon said, with a double "!!". Entering "PE operation on a non PE file" in the search engine of your choice would have pointed you right at the relevant information on this very website.

2) What Rudster816 said. You could also check out dev86, a dedicated 16bit toolchain.

Re: Annoying ld linking problem

Posted: Mon Apr 16, 2012 5:10 pm
by drunkenfox
Alright got this fixed. All I had to do is switch to DJGPP.

Re: Annoying ld linking problem

Posted: Mon Apr 16, 2012 5:20 pm
by Hoozim
Cygwin works fine, you need a cross-compiler. A cross-compiler is very simple to create. As pointed to above, there is an entire article on how to create one. There is also an article on why one is necessary.

Re: Annoying ld linking problem

Posted: Tue Apr 17, 2012 2:32 am
by Combuster
ponyboy wrote:All I had to do is switch to DJGPP.
That's bound to give you many more problems later.