Search code examples
cassemblygccx86

Would compiler put jump table at the end of function?


For example, this is an assembly with jump table

movl    $5, -4(%ebp)
cmpl    $10, -4(%ebp)
ja  L13
movl    -4(%ebp), %eax
sall    $2, %eax
movl    L14(%eax), %eax
jmp *%eax
.section .rdata,"dr"
.align 4
L14:
.long   L13
.long   L3
.long   L4
.long   L5
.long   L6
.long   L7
.long   L8
.long   L9
.long   L10
.long   L11
.long   L12
.text
L3:
movl    $LC0, (%esp)
call    _printf
jmp L2
...

My question is, is that possible for compiler like GCC or ICC to put jump table at the end of the function instead of in the middle of function?


Solution

  • The table won't end up in the middle of the function. If you look carefully you may notice this line:

    .section .rdata,"dr"
    

    It tells the assembler to put the following data into the section named ".rdata".

    And the following .text tells the assembler to switch back to the .text section. So the function code will actually be placed contiguously (in .text), and the jump table will be stored separately (in .rdata).