void* curbrk;
__asm__ __volatile__(
"mov .curbrk, %%rax;"
"mov %%rax, %0"
: "=r" (curbrk)
:: "%rax"
);
Can anyone explain what this simple assembly code does? Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It copies the value of a symbol
.curbrk, probably defined somewhere on assembly or a linker script into the C variablecurbrk, clobbering theRAXregister in the process..curbrkprobably points to the current end of the data segment. Glibc appears to define a similar symbol__curbrk, you are probably using some other libc (BSD?). In any case,sbrk(0)would be a more portable way of accessing that value.After looking at the FreeBSD crossreference, I can say it indeed points to the current end of the data segment: it is used both in
brk()andsbrk(), using the HIDENAME macro to prepend a., and it appears on amd64’s System.map (no longer true on current FreeBSD).Note, however, that in newer FreeBSD,
brk()andsbrk()have been reimplemented to no longer depend of.curbrk, which was initialized from_end, which was supposed to come from the executable, but there was trouble when mixing LLVM’s ld and GNU ld. Sobrk()andsbrk()now use the kernel to initialize its internalcurbrk, and no longer depend on_end. See FreeBSD PR228754 for details.