When dealing with MAGIC, you have to remember that its a linked list and a "thing" may have more than one form of magic applied. You have to walk the MAGIC/MOREMAGIC list.
Finding out that %imahash is the name of lexical (or global) that was passed in is not trivial. In essense your job becomes: examine your calling context's pad and see if the hash is present there. If so, that's the name. Otherwise you have to find it in the symbol table by doing a search. If that fails then the hash might be present in a Lexical::Typeglob and you aren't going to find it unless you repeat the search except examing the contents of typeglob references as well.