An ELF format file stores the type of architecture. There is a dictionary in pyelftools :

 ENUM_E_MACHINE = dict ( EM_NONE = 0, # No machine EM_M32 = 1, # AT&T WE 32100 EM_SPARC = 2, # SPARC ... ) 

The user interface is adapted to return the strings from this enum .

But it is required to get number from the dictionary (it turns out to get a line). For example :

 print something.elf['e_machine'] print something.elf.header.e_machine 

It displays the string. For example, EM_X86_64 or EM_M32 . How does this library directly or indirectly get a number from the dictionary that corresponds to the type of machine, and not a string?

  • does the option follow from what has been said: ENUM_E_MACHINE[something.elf['e_machine'] ] ? - Vasyl Kolomiets

1 answer 1

If something.elf['e_machine'] == 'EM_M32' , and you want to get 1 , then just like with a regular dictionary, get the value by the key value = d[key] :

 print(ENUM_E_MACHINE[something.elf['e_machine']]) # -> 1 
  • damn @jfs - wrote off the answer from my comment, so at least I like someone on the comment throw someone else ?! Where the world is heading ;-) - Vasyl Kolomiets
  • @VasylKolomiets if you think that I need to read comments in order to answer such a question, then you are mistaken :) It never ceases to amaze at what simple code people stumble. And the answers should be written in the answers. - jfs
  • ! whenever I answer such questions - I was minus. I decided - what to answer correctly in the comments ... Well, okay. Happy Holidays)))) - Vasyl Kolomiets
  • one
    @VasylKolomiets: and this answer, too, minus set. Do not pay attention, the main thing that the author of the question and future readers from Google with a similar problem your answers helped. - jfs