GCC cutting of higher 32 bits of qword in dlmalloc port

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
Qeroq
Member
Member
Posts: 52
Joined: Wed Aug 25, 2010 6:35 am
Location: Bonn, Germany

GCC cutting of higher 32 bits of qword in dlmalloc port

Post by Qeroq »

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: Select all

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: Select all

  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: Select all

      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: Select all

    -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)
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: GCC cutting of higher 32 bits of qword in dlmalloc port

Post by qw »

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.
Qeroq
Member
Member
Posts: 52
Joined: Wed Aug 25, 2010 6:35 am
Location: Bonn, Germany

Re: GCC cutting of higher 32 bits of qword in dlmalloc port

Post by Qeroq »

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

Re: GCC cutting of higher 32 bits of qword in dlmalloc port

Post by Solar »

My project Makefile uses, among others:

Code: Select all

-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.
User avatar
qw
Member
Member
Posts: 792
Joined: Mon Jan 26, 2009 2:48 am

Re: GCC cutting of higher 32 bits of qword in dlmalloc port

Post by qw »

I call this one solved. Glad I could help.

Roel
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: GCC cutting of higher 32 bits of qword in dlmalloc port

Post by Combuster »

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 ]
Post Reply