This is an archive of the discontinued LLVM Phabricator instance.

Added ELFObjectFileBase::checkMagic() for checking ELF magic word.
AbandonedPublic

Authored by vzakhari on Jun 2 2021, 11:43 AM.

Details

Summary

Added new method to ELFObjectFileBase to check whether the ELF image used to construct it is actually an ELF image (i.e. it has ELF magic word).

Diff Detail

Event Timeline

vzakhari requested review of this revision.Jun 2 2021, 11:43 AM
vzakhari created this revision.
Herald added a project: Restricted Project. · View Herald TranscriptJun 2 2021, 11:43 AM

git-clang-format HEAD^ may be useful for reformatting the already changed parts. LGTM, @jhenderson?

vzakhari updated this revision to Diff 349923.Jun 4 2021, 11:08 AM

We have lib/BinaryFormat/Magic.cpp. Do we need the ELF specific API?

We have lib/BinaryFormat/Magic.cpp. Do we need the ELF specific API?

Thanks! I was not aware of this code. How about adding something like static inline is_any_elf(file_magic) method in Magic.h? If you agree, then I will abandon this change-set and add the new method in D103545.

We have lib/BinaryFormat/Magic.cpp. Do we need the ELF specific API?

Thanks! I was not aware of this code. How about adding something like static inline is_any_elf(file_magic) method in Magic.h? If you agree, then I will abandon this change-set and add the new method in D103545.

I'm assuming this would be to avoid having to write something like if (objectType == elf || objectType == elf_relocatable || /*all the other elf_* types*/ ...) (or an equivalent switch statement)? Is this something you'd use in more than one place within your code?

We have lib/BinaryFormat/Magic.cpp. Do we need the ELF specific API?

Thanks! I was not aware of this code. How about adding something like static inline is_any_elf(file_magic) method in Magic.h? If you agree, then I will abandon this change-set and add the new method in D103545.

I'm assuming this would be to avoid having to write something like if (objectType == elf || objectType == elf_relocatable || /*all the other elf_* types*/ ...) (or an equivalent switch statement)? Is this something you'd use in more than one place within your code?

Exactly! I could add the same helper in the plugin code, but I'd rather keep it close to the file_magic enum definition. Do you suggest adding it in the plugin code?

It is rare that an application can process all of elf_relocatable, elf_executable, and elf_shared_object. llvm-objcopy is such a tool, but most others either only handle elf_relocatable or elf_executable+elf_shared_object.

vzakhari abandoned this revision.Jun 7 2021, 3:37 PM

Implemented using lib/BinaryFormat/Magic.cpp