Ajay
Ajay

Reputation: 2078

How would I duplicate and rename symbols in object file?

I have an object file (for which I don't have access to the source).

For good reasons, I need to duplicate a call path. For example, this object file might have the following symbols:

_FuncA _FuncB _FuncC

_FuncA calls _FuncB, which in turns calls _FuncC. FuncC might increment a global variable defined in the C source code counter.

I want to modify this object file and duplicate _FuncA, _FuncB, and _FuncC.

The result would be an object file with the following symbols:

_FuncA _FuncB _FuncC _FuncA_copy _FuncB_copy _FuncC_copy

_FuncA_copy would need to call _FuncB_copy, which in turns calls _FuncC_copy. And I need _FuncC_copy to still reference the same global variable counter and increment it.

What I have so far:

It seems like the objcopy command will let you add new symbols using the flag --add-symbol <name>=[<section>:]<value>[,<flags>].

This seems like it would help me create _FuncA_copy, _FuncB_copy, _FuncC_copy. But is there anyway to modify the function call inside _FuncA_copy to _FuncB to go to _FuncB_copy instead?

Is there a better way to do this?

Upvotes: 3

Views: 1421

Answers (1)

Ajay
Ajay

Reputation: 2078

The solution I found is writing an LLVM Pass shared module that can plugin to LLVM's optimizer tool which takes in a bytecode object and outputs a bytecode object.

http://llvm.org/docs/WritingAnLLVMPass.html

A LLVM pass is a custom optimizer that you can create, and within it you can have the optimizer loop over all function symbols and rename them as you wish and it updates all of the references to that function.

Upvotes: 4

Related Questions