1

Suppose, I have the following source code:

contract Test {
    function add(uint a, uint b) public returns(uint) {
        return a + b;
    }

}

And bytecode after compilation:

6080604052348015600f57600080fd5b506004361060285760003560e01c8063771602f714602d575b600080fd5b603c6038366004606b565b604e565b60405190815260200160405180910390f35b6000605a8261012360a2565b6062908460b6565b90505b92915050565b60008060408385031215607d57600080fd5b50508035926020909101359150565b634e487b7160e01b600052601160045260246000fd5b80820281158282048414176065576065608c565b808201808211156065576065608c56fea2646970667358221220b0f48d23e80101aadbaa776a9598f8daa1c0f16cd5b1b2613aecee6e837009dc64736f6c63430008110033

Is it possible to get offset of add function in this binary data? I.e. offset to place, where all parameters are loaded from calldata and placed to the stack.
If it is impossible currently, could you please point me out in the Solidity compiler source code where I can get these offsets?

1 Answer 1

0
  1. Take the function signature: add(uint256,uint256)
  2. Compute with Keccak256
  3. Take the first 4 bytes of the hash 0x77|16|02|f7

6080604052348015600f57600080fd5b506004361060285760003560e01c8063771602f714602d575b600080fd5b603c6038366004606b565b604e565b60405190815260200160405180910390f35b6000605a8261012360a2565b6062908460b6565b90505b92915050565b60008060408385031215607d57600080fd5b50508035926020909101359150565b634e487b7160e01b600052601160045260246000fd5b80820281158282048414176065576065608c565b808201808211156065576065608c56fea2646970667358221220b0f48d23e80101aadbaa776a9598f8daa1c0f16cd5b1b2613aecee6e837009dc64736f6c63430008110033

1
  • Please note that this is just a very rough heuristic, not something to rely on as foolproof. It will take you to the spot where the function selector is loaded on the stack but only as long as 771602f7 does not pop up somewhere else in the bytecode. The jump to the internal function code happens somewhere after the selector but there may be quite a few things in between. Also the function could be inlined. In the end bytecode is just an amorphous blob. There are no real functions, they're just a convention. This will only change when we get EOF.
    – cameel
    Mar 26, 2023 at 20:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.