Summary: | Header issue in sys-devel/gcc-4.1.2 / sys-libs/glibc-2.6.1 | ||
---|---|---|---|
Product: | Gentoo Linux | Reporter: | Alessandro Di Marco <dmr> |
Component: | [OLD] Core system | Assignee: | Gentoo Toolchain Maintainers <toolchain> |
Status: | RESOLVED INVALID | ||
Severity: | critical | CC: | amd64, dmr |
Priority: | High | ||
Version: | 2008.0 | ||
Hardware: | AMD64 | ||
OS: | Linux | ||
Whiteboard: | |||
Package list: | Runtime testing required: | --- |
Description
Alessandro Di Marco
2009-02-11 23:16:57 UTC
I think to have discovered what happens. When the #include is commented out, the compiler uses the 32bit funcall in place of the needed 64bit one. Here below is the corresponding assembly snippet for both versions: /* #include <arpa/inet.h> */ movl $16777343, -16(%rbp) movl -16(%rbp), %edi movl $0, %eax call inet_ntoa cltq movq %rax, -8(%rbp) movq -8(%rbp), %rdi call puts movl $0, %eax #include <arpa/inet.h> movl $16777343, -16(%rbp) movl -16(%rbp), %edi call inet_ntoa movq %rax, -8(%rbp) movq -8(%rbp), %rdi call puts movl $0, %eax As you can see, the #include-less version uses the cltq (aka cdqe) to convert the doubleword in eax to a quadword in rax with the same numerical value. Probably this value is not as good as it should (see below) 4 /* uncomment me to solve the problem! */ 5 /* #include <arpa/inet.h> */ 6 7 int main(void) 8 { 9 struct in_addr io = { 0x100007f }; 10 char *x = inet_ntoa(io); 11 12 printf("%s\n", x); (gdb) s 9 struct in_addr io = { 0x100007f }; (gdb) s 10 char *x = inet_ntoa(io); (gdb) s 12 printf("%s\n", x); (gdb) p x $1 = 0x478e76c0 <Address 0x478e76c0 out of bounds> OK, I solved. Without the #include, gcc assumes that inet_ntoa() returns an integer (4 bytes). Differently on x86, this is unable to store memory addresses (char * is 8 bytes long in amd64 architecture), so the cltq instruction does nothing but extending an already truncated address and the printf segfaults. Sorry for any inconvenience you probably ignored the warnings. implicit decls -> int return type. Yeah, usually I don't! Since the snippet came by a well-known network book, this time I "forgot" the -Wall.. |