This is a grammatically correct sentence:
It is him who carried a gun.
But this one:
I killed him who carried a gun.
sounds a little odd.
Is this sentence grammatically correct? If not, what is the problem?
Native speakers will say:
It's him who had the gun.colloquial
It is he who had the gun.formal
Your second sentence would be unusual. Instead of him, we would probably say "the man" or "the one".
There were two bank robbers. One of them was unarmed. The police shot the one who had a gun.
You will find examples of a relative clause headed by who modifying him, but this does not occur in contemporary conversational English; today it appears in literary contexts emulating older forms of the language:
Let him who is without sin cast the first stone.