logoalt Hacker News

tpoacher • 08/15/2026 • 4 replies • view on HN

What's a "trampoline"?


Replies

jcranmer • 08/15/2026

In this context:

Nested functions have a different ABI from regular C functions, due to the invisible static chain register that needs to be set up. C has no way of indicating this different ABI, so GCC happily lets you cast a nested function to a C function pointer by creating a little tiny function that puts the right value in the static chain register before calling the nested function. This little tiny function is the trampoline.

Since the trampoline needs to live somewhere, GCC puts it on the stack, requiring the stack to be executable and consequently a whole lot of people hate the feature because it's a walking security nightmare.

➕ show 3 replies
mananaysiempre • 08/15/2026

Could be a number of things depending on context. In this case it’s a short function that adjusts some things and jumps to the actual functions (a “thunk” is another term for this). Specifically, if in GCC you write

  int f(int x) {
      int g(int y) { ... use x and y ... }
      ...
      h(&g);
      ...
  }
then what the compiled code for f does is construct on the stack a short piece of machine code:

  mov <well-known register>, <frame pointer>
  jmp <start of g’s code>
and &g points to the start not of g’s code but of this snippet on the stack, which has the parent function’s frame pointer compiled into it as a literal constant. The snippet is called a trampoline.
monster_truck • 08/15/2026

It's where you jump and then get immediately bounced back. Basically GOTOs with params

➕ show 1 reply