MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / ParseHeaders

Method ParseHeaders

view/elf/elfview.cpp:2844–2975  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2842
2843
2844uint64_t ElfViewType::ParseHeaders(BinaryView* data, ElfIdent& ident, ElfCommonHeader& commonHeader, Elf64Header& header, Ref<Architecture>* arch, Ref<Platform>* plat, string& errorMsg, BNEndianness& endianness)
2845{
2846 if (!IsTypeValidForData(data))
2847 {
2848 errorMsg = "invalid signature";
2849 return 0;
2850 }
2851
2852 // parse ElfIdent
2853 if (data->Read(&ident, 0, sizeof(ident)) != sizeof(ident))
2854 {
2855 errorMsg = "unable to read header";
2856 return 0;
2857 }
2858
2859 BinaryReader reader(data);
2860 if (ident.encoding <= 1)
2861 endianness = LittleEndian;
2862 else if (ident.encoding == 2)
2863 endianness = BigEndian;
2864 else
2865 {
2866 errorMsg = "invalid encoding";
2867 return 0;
2868 }
2869
2870 // parse ElfCommonHeader
2871 reader.SetEndianness(endianness);
2872 reader.Seek(sizeof(ident));
2873 commonHeader.type = reader.Read16();
2874 commonHeader.arch = reader.Read16();
2875 commonHeader.version = reader.Read32();
2876
2877 // Promote the file class to 64-bit
2878 // TODO potentially add a setting to allow the user to override header interpretation
2879 if ((commonHeader.type == ET_EXEC) && (commonHeader.arch == EM_X86_64) && (ident.fileClass == 1))
2880 {
2881 ident.fileClass = 2;
2882 m_logger->LogWarn(
2883 "Executable file claims to be 32-bit but specifies a 64-bit architecture. It is likely malformed or "
2884 "malicious. Treating it as 64-bit.");
2885 }
2886
2887 // parse Elf64Header
2888 if (ident.fileClass == 1) // 32-bit ELF
2889 {
2890 header.entry = reader.Read32();
2891 header.programHeaderOffset = reader.Read32();
2892 header.sectionHeaderOffset = reader.Read32();
2893 header.flags = reader.Read32();
2894 header.headerSize = reader.Read16();
2895 header.programHeaderSize = reader.Read16();
2896 header.programHeaderCount = reader.Read16();
2897 header.sectionHeaderSize = reader.Read16();
2898 header.sectionHeaderCount = reader.Read16();
2899 header.stringTable = reader.Read16();
2900 }
2901 else if (ident.fileClass == 2) // 64-bit ELF

Callers 1

ElfViewMethod · 0.45

Calls 11

LogWarnMethod · 0.80
Get<bool>Method · 0.80
RecognizePlatformMethod · 0.80
ReadMethod · 0.45
SetEndiannessMethod · 0.45
SeekMethod · 0.45
Read16Method · 0.45
Read32Method · 0.45
Read64Method · 0.45
GetArchitectureMethod · 0.45
GetOffsetMethod · 0.45

Tested by

no test coverage detected