0

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?

nrz
  • 10,435
  • 4
  • 39
  • 71
  • Depends on the function really. If there is a dependency most likely it will be placed in the middle. It can really only be placed at the end if it's the last thing executed by a function (that isn't inlined). – Jesus Ramos Nov 07 '13 at 22:20
  • Where the compiler places the JMP table is generally a matter of convenience for the compiler; whoever wrote it had reasons for placing it there. It doesn't matter a lot unless you are really concerned about interference of different cache lines. – Ira Baxter Nov 07 '13 at 22:28

1 Answers1

5

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).

Igor Skochinsky
  • 24,629
  • 2
  • 72
  • 109