A production issue has led our team to the following questions:
ntohs
and ntohl
implemented?I know the implications behind questions may seem far-fetched and ridiculous, but I have been asked to investigate.
The hardware in question is an Intel box, little endian, 64-bit processor and compiled in 64 bit.
Do the following:
#include <arpa/inet.h>
int main()
{
volatile uint32_t x = 0x12345678;
x = ntohl(x);
return 0;
}
Then compile with:
$ gcc -O3 -g -save-temps test.c
And analyze the resulting test.s
file, or alternatively run objdump -S test.o
.
In my machine (Ubuntu 13.4) the relevant asssembler is:
movl $305419896, 12(%esp)
movl 12(%esp), %eax
bswap %eax
movl %eax, 12(%esp)
Hints:
12(%esp)
is the address of the volatile variable.movl
instructions are there for the volatile
-ness of x
. The only really interesting instruction is bswap
.ntohl
is compiled as an inline-intrinsic.Moreover, if I look at the test.i
(precompiled output), I find that the ntohl
is #defined
as simply __bswap_32()
, which is an inline function with just a call to __builtin_bswap32()
.
/usr/include/bits/byteswap.h
for the __bswap_16
and __bswap_32
functions, which are used when optimization is enabled (see <netinet/in.h>
for details of how.)-save-temps
option to keep the intermediate .s
files, or use -S
to stop after compilation and before assembling the code, or use http://gcc.godbolt.org/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With