puzzled assemble codes in Linux

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
cxsnew

puzzled assemble codes in Linux

Post by cxsnew »

All,
Can you give some hints about the following assembly codes in Linux:
#define __build_read_lock_ptr(rw, helper) \
   asm volatile(LOCK "subl $1,(%0)\n\t" \
       "js 2f\n" \
       "1:\n" \
       ".section .text.lock,\"ax\"\n" \
       "2:\tcall " helper "\n\t" \
       "jmp 1b\n" \
       ".previous" \
       ::"a" (rw) : "memory")

What does the .section .text.lock and .previous mean?
Can somebody give some information where to find information about .previous usage?
Thanks!
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:puzzled assemble codes in Linux

Post by Pype.Clicker »

well... let's extract the ASM code out of that ugly C encoding ...

Code: Select all

   lock subl $1,(%0)
   js 2f   ;; "2" is the label and must be forward ...
1:
.section .text.lock,"ax"
2:
   call <helper>
   jmp 1b ;; "1" is the label and must be backward
.previous
.section and .previous are GAS directives ... Quoting "info as":
`.previous'
===========

This is one of the ELF section stack manipulation directives. The others are `.section' (*note Section::), `.subsection' (*note
SubSection::), `.pushsection' (*note PushSection::), and `.popsection' (*note PopSection::).
The "ax" string after ".text.lock" section name does *not* refer to the accumulator register, but instead says that the section is Allocatable and eXecutable. Some section names have default flags, but .text.lock is SomethingSpecial introduced by Linux designers, so it needs to have explicit bits...
cxsnew

Re:puzzled assemble codes in Linux

Post by cxsnew »

But it seems that:
2->1->2->1->... or 1->2->1->2->...
bad loop struct? Then how can the function return to the caller? ;D
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:puzzled assemble codes in Linux

Post by Pype.Clicker »

i guess the trick here is that the '2:' label is *not* contiguous from '1:', as it's in a distinct section, so when you 'jmp 1b', you actually don't re-enter the loop but rather continue normal operations.

The best is to assemble it, and give a look at objdump -drS output, but it should give something like ...

.text section:

Code: Select all

    ;; code before build_read_lock()
    lock subl ...
    js some_position_in_lock_text
come_back_here:
    ;; code after build_read_lock()

    ;; more code
.lock.text section

Code: Select all

   ;; more code
some_position_in_lock_text:    
   call <helper-function>
   jmp come_back_here

   ;; more code
cxsnew

Re:puzzled assemble codes in Linux

Post by cxsnew »

Yes, you are right, thanks a lot!
I looked up some reference books, indeed the lable "1" and "2" are not in the same section, so when jmp to lable "1" will not enter lable "2" again, it will continue execute the codes following lable "1" in a different section.
Thanks! ;D
Post Reply