Page 1 of 1

GCC cutting of higher 32 bits of qword in dlmalloc port

Posted: Fri Feb 11, 2011 8:17 am
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?

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

Posted: Fri Feb 11, 2011 9:11 am
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.

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

Posted: Fri Feb 11, 2011 10:09 am
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?

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

Posted: Fri Feb 11, 2011 10:24 am
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.

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

Posted: Fri Feb 11, 2011 1:05 pm
by qw
I call this one solved. Glad I could help.

Roel

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

Posted: Fri Feb 11, 2011 4:44 pm
by Combuster
berkus wrote:add -Werror
Amen. It will make you a much better developer.