This class assists in inserting MI thunk functions into the module and rewriting the existing machine functions to call these thunks.

Synopsis

Declared in <llvm/CodeGen/IndirectThunks.h>

template<
    typename Derived,
    typename InsertedThunksTy = bool>
class ThunkInserter;

Description

One of the common cases is implementing security mitigations that involve replacing some machine code patterns with calls to special thunk functions.

Inserting a module pass late in the codegen pipeline may increase memory usage, as it serializes the transformations and forces preceding passes to produce machine code for all functions before running the module pass. For that reason, ThunkInserter can be driven by a MachineFunctionPass by passing one MachineFunction at a time to its run(MMI, MF) method. Then, the derived class should * call createThunkFunction from its insertThunks method exactly once for each of the thunk functions to be inserted * populate the thunk in its populateThunk method

Note that if some other pass is responsible for rewriting the functions, the insertThunks method may simply create all possible thunks at once, probably postponed until the first occurrence of possibly affected MF.

Alternatively, insertThunks method can rewrite MF by itself and only insert the thunks being called. In that case InsertedThunks variable can be used to track which thunks were already inserted.

In any case, the thunk function has to be inserted on behalf of some other function and then populated on its own "iteration" later ‐ this is because MachineFunctionPass will see the newly created functions, but they first have to go through the preceding passes from the same pass manager, possibly even through the instruction selector.

Member Functions

Name

Description

init

Reset inserted‐thunk state and run derived initialization on M.

run

Insert or populate thunks for MF; returns true if MMI or MF changed.

Protected Member Functions

Name

Description

createThunkFunction

Create an empty thunk function.

doInitialization

Initializes thunk inserter.

getThunkPrefix

Returns common prefix for thunk function's names.

insertThunks

Rewrites the function if necessary, returns the set of thunks added.

mayUseThunk

Checks if MF may use thunks (true ‐ maybe, false ‐ definitely not).

populateThunk

Populate the thunk function with instructions.

Created with MrDocs