Annoying ld linking problem

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
User avatar
drunkenfox
Member
Member
Posts: 46
Joined: Tue Mar 13, 2012 10:46 pm

Annoying ld linking problem

Post 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.
;goodbye OS, hello BIOS
mov eax, FFFFFFF0h
jmp eax
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: Annoying ld linking problem

Post 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
User avatar
drunkenfox
Member
Member
Posts: 46
Joined: Tue Mar 13, 2012 10:46 pm

Re: Annoying ld linking problem

Post 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.
;goodbye OS, hello BIOS
mov eax, FFFFFFF0h
jmp eax
User avatar
brain
Member
Member
Posts: 234
Joined: Thu Nov 05, 2009 5:04 pm
Location: UK
Contact:

Re: Annoying ld linking problem

Post 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.
User avatar
drunkenfox
Member
Member
Posts: 46
Joined: Tue Mar 13, 2012 10:46 pm

Re: Annoying ld linking problem

Post by drunkenfox »

It's going to be the real mode os loader after loaded at 0x500 by broken thorn's bootsector.
;goodbye OS, hello BIOS
mov eax, FFFFFFF0h
jmp eax
Rudster816
Member
Member
Posts: 141
Joined: Thu Jun 17, 2010 2:36 am

Re: Annoying ld linking problem

Post 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.
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Annoying ld linking problem

Post 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.
Every good solution is obvious once you've found it.
User avatar
drunkenfox
Member
Member
Posts: 46
Joined: Tue Mar 13, 2012 10:46 pm

Re: Annoying ld linking problem

Post by drunkenfox »

Alright got this fixed. All I had to do is switch to DJGPP.
;goodbye OS, hello BIOS
mov eax, FFFFFFF0h
jmp eax
Hoozim
Member
Member
Posts: 53
Joined: Fri Jul 23, 2010 8:26 am

Re: Annoying ld linking problem

Post 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.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Annoying ld linking problem

Post by Combuster »

ponyboy wrote:All I had to do is switch to DJGPP.
That's bound to give you many more problems later.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply