64bit printf

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
blound
Member
Member
Posts: 70
Joined: Sat Dec 01, 2007 1:36 pm

64bit printf

Post by blound »

I am having a problem with my 64 bit kernel and printf. I have tested my puts and putch routines to death, and have put in my own basic printhex/printdecimal wrapper functions around them and they work. the problem is that when I do simple printfs I get random values from the stack.
In my boot assembly file I declare my stack as:

Code: Select all

kernel_stack:
		times 32768 db 0xcc
to catch errors. Now in my kmain I have:

Code: Select all

int r = 0x33413442;
	
puts("\n-----\n");
puts((char *)&r);
puts("\n-----\n");
	
printf("r is %x\n",r);
(printf ideals with the va_list etc then sends the final string to puts)

and the puts routine prints 'r' properly but printf prints 0xcccccccc which is somewhere random on the stack. I have tried many different combinations of print modifiers and none of them work. I have also tried moving where the variable im printing is located (bss/data) etc and same results.

I don't think its stack corruption on my side because puts is working ifne otherwise.

I was also wondering what should be the proper type for rounding inside of the va_ functions. 32 bit systems use size of (4 bytes), but shouldnt 64 bit systems use sizeof(long) (8 bytes)? I tried both and neither fixed my problem.

Does someone have a working 64bit printf so I can test to make sure i am not completely off or has anyone else had problems with this? I have tried 3 different printf implementations and all have the same problem.
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

Post by xyzzy »

I'm using the vsnprintf function from Linux, which works fine on both my IA32 and x86_64 ports.

The vsnprintf function is here and the kprintf function is here
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post by Korona »

The va_arg macros on x86-64 don't work like the va_arg macros in 32bit mode. Try to use the va_arg macros from a working system. (e.g. copy them from linux)
xyzzy
Member
Member
Posts: 391
Joined: Wed Jul 25, 2007 8:45 am
Libera.chat IRC: aejsmith
Location: London, UK
Contact:

Post by xyzzy »

Korona wrote:The va_arg macros on x86-64 don't work like the va_arg macros in 32bit mode. Try to use the va_arg macros from a working system. (e.g. copy them from linux)
I personally find it easier to just use the GCC builtins for it (__builtin_va_arg, __builtin_va_start, __builtin_va_end).
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post by Korona »

IIRC I also used them when I was coding in C, but they don't appear in the latest gcc manuals so I thought that they were deprecated.
Post Reply