Sebi
Sebi

Reputation: 4522

Can llvm emit code that jumps to a given address within a function?

Following up on this question, is it possible for llvm to generate code that may jump to an arbitrary address within a function in the same address space? i.e.

      void func1() {
       ...
       <code that jumps to addr2>
       ...
       }

       void func2() {
       ...
addr2:
       <some code in func2()>
       ...
       }

Upvotes: 3

Views: 797

Answers (1)

MikeMB
MikeMB

Reputation: 21156

Yes,No,Yes,No,(yes) - It depends on the level you look at and what you mean with possible:

  • Yes, as the llvm backend will produce target specific assembler instructions and those assembler instructions allow to set the program counter to an abitrary value.
  • No, because - as far as I know - the llvm ir (the intermediate representation into which a frontend like clang compiles your c code) hasn't any instructions that would allow abitrary jumps between (llvm-ir) functions.
  • Yes, because the frontend COULD certainly produce code, that simulates that behaviour (breaking up func2 into multiple separate functions).
  • No, because C and C++ don't allow such jumps to ARBITRARY positions and so clang will not compile any program that tries to do that (e.g. via goto)
  • (yes) the c longjmp macro jumps back to a place in the control flow that you have already visited (where you called setjmp) but also restores (most) of the system state. EDIT: However, this is UB if func2 isn't somewhere up in the current callstack from where you jump.

Upvotes: 3

Related Questions