OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 3:59 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: GCC cutting of higher 32 bits of qword in dlmalloc port
PostPosted: Fri Feb 11, 2011 8:17 am 
Offline
Member
Member

Joined: Wed Aug 25, 2010 6:35 am
Posts: 52
Location: Bonn, Germany
Hello,
porting dlmalloc (2.8.4) to my OS, I've encountered a very strange error: My sbrk function returns a void*, which is 8 bytes in size (I checked on the type almost 20 times), but the higher dword is stripped away in the dlmalloc code. To check what caused the error I compiled the following C code in both my main.c, where everything works correctly, and directly at the top of dlmalloc.c (after the required imports for types and console):
Code:
uintptr_t result = heap_sbrk(0x2000);
console_print_hex(result);

In my main.c this generates the following byte code, which seems perfectly fine to me:
Code:
  5a:   bf 00 20 00 00          mov    $0x2000,%edi
  5f:   48 b8 00 00 00 00 00    movabs $0x0,%rax
  66:   00 00 00
  69:   ff d0                   callq  *%rax
  6b:   48 89 45 f8             mov    %rax,-0x8(%rbp)
  6f:   48 8b 45 f8             mov    -0x8(%rbp),%rax
  73:   48 89 c7                mov    %rax,%rdi
  76:   48 b8 00 00 00 00 00    movabs $0x0,%rax
  7d:   00 00 00
  80:   ff d0                   callq  *%rax

But in dlmalloc.c this byte code is generated:
Code:
      12:   bf 00 20 00 00          mov    $0x2000,%edi
      17:   b8 00 00 00 00          mov    $0x0,%eax
      1c:   48 ba 00 00 00 00 00    movabs $0x0,%rdx
      23:   00 00 00
      26:   ff d2                   callq  *%rdx
      28:   48 98                   cltq   
      2a:   48 89 45 f8             mov    %rax,-0x8(%rbp)
      2e:   48 8b 45 f8             mov    -0x8(%rbp),%rax
      32:   48 89 c7                mov    %rax,%rdi
      35:   48 b8 00 00 00 00 00    movabs $0x0,%rax
      3c:   00 00 00
      3f:   ff d0                   callq  *%rax

For some reason there is this strange cltq that copies only the half of sbrk's result.

Both files are compiled with the same flags:
Code:
    -m64 \
    -nostdlib -nostdinc -fno-builtin -fno-stack-protector \
    -D__AMD64__ \
    -D__DEBUG__ \
    -x c \
    -mcmodel=large \
    -I./src/


Does anyone of you have an idea what could have caused this?

_________________
https://github.com/qero/Hydrogen (Loader for AMD64 kernels running on top of GRUB2)


Top
 Profile  
 
 Post subject: Re: GCC cutting of higher 32 bits of qword in dlmalloc port
PostPosted: Fri Feb 11, 2011 9:11 am 
Offline
Member
Member
User avatar

Joined: Mon Jan 26, 2009 2:48 am
Posts: 792
My guess is that "heap_sbrk" is not properly declared in the latter case, and the compiler assumes it is returning an int (32 bits) instead of an uintptr_t (64 bits). Check the inclusion of the header file.


Top
 Profile  
 
 Post subject: Re: GCC cutting of higher 32 bits of qword in dlmalloc port
PostPosted: Fri Feb 11, 2011 10:09 am 
Offline
Member
Member

Joined: Wed Aug 25, 2010 6:35 am
Posts: 52
Location: Bonn, Germany
Oh, yeah, pretty obvious: I refactored some of the heap code in earlier debugging moving heap_sbrk into another header, which i did not include... #-o

Is there a way to configure gcc to print a warning if a function is neither defined or extern?

_________________
https://github.com/qero/Hydrogen (Loader for AMD64 kernels running on top of GRUB2)


Top
 Profile  
 
 Post subject: Re: GCC cutting of higher 32 bits of qword in dlmalloc port
PostPosted: Fri Feb 11, 2011 10:24 am 
Offline
Member
Member
User avatar

Joined: Thu Nov 16, 2006 12:01 pm
Posts: 7614
Location: Germany
My project Makefile uses, among others:

Code:
-Wall -Wextra -pedantic -Wshadow -Wpointer-arith -Wcast-align -Wwrite-strings -Wmissing-prototypes -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -Wuninitialized


Between -Wmissing-prototypes, -Wmissing-declarations and -Wredundant-decls, most of your header woes should be detected at compile time.

_________________
Every good solution is obvious once you've found it.


Top
 Profile  
 
 Post subject: Re: GCC cutting of higher 32 bits of qword in dlmalloc port
PostPosted: Fri Feb 11, 2011 1:05 pm 
Offline
Member
Member
User avatar

Joined: Mon Jan 26, 2009 2:48 am
Posts: 792
I call this one solved. Glad I could help.

Roel


Top
 Profile  
 
 Post subject: Re: GCC cutting of higher 32 bits of qword in dlmalloc port
PostPosted: Fri Feb 11, 2011 4:44 pm 
Offline
Member
Member
User avatar

Joined: Wed Oct 18, 2006 3:45 am
Posts: 9301
Location: On the balcony, where I can actually keep 1½m distance
berkus wrote:
add -Werror
Amen. It will make you a much better developer.

_________________
"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 ]


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], suspectedowl and 156 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group