/* * Copyright (C) 2022-2023 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once #include "shared/source/device_binary_format/zebin/zebin_elf.h" #include "shared/source/utilities/arrayref.h" #include "shared/source/utilities/const_stringref.h" #include #include #include #include class OclocArgHelper; struct IgaWrapper; namespace NEO { namespace Elf { struct IntelGTNote; template struct Elf; template struct ElfEncoder; } // namespace Elf namespace Zebin::Manipulator { struct SectionInfo { std::string name; uint32_t type; }; struct Arguments { std::string pathToDump = ""; std::string binaryFile = ""; bool showHelp = false; bool skipIGAdisassembly = false; }; enum BinaryFormats { PatchTokens, Zebin32b, Zebin64b }; using ErrorCode = int; ErrorCode parseIntelGTNotesSectionForDevice(const std::vector &intelGTNotes, IgaWrapper *iga, OclocArgHelper *argHelper); ErrorCode validateInput(const std::vector &args, IgaWrapper *iga, OclocArgHelper *argHelper, Arguments &outArguments); BinaryFormats getBinaryFormatForAssemble(OclocArgHelper *argHelper, const std::vector &args); BinaryFormats getBinaryFormatForDisassemble(OclocArgHelper *argHelper, const std::vector &args); bool is64BitZebin(OclocArgHelper *argHelper, const std::string §ionsInfoFilepath); constexpr ConstStringRef sectionsInfoFilename = "sections.txt"; template class ZebinDecoder { public: using ElfT = Elf::Elf; using ElfRelT = Elf::ElfRel; using ElfRelaT = Elf::ElfRela; using ElfSymT = Elf::ElfSymbolEntry; ZebinDecoder(OclocArgHelper *argHelper); ~ZebinDecoder(); ErrorCode decode(); ErrorCode validateInput(const std::vector &args); void printHelp(); protected: MOCKABLE_VIRTUAL ErrorCode decodeZebin(ArrayRef zebin, ElfT &outElf); MOCKABLE_VIRTUAL void dump(ConstStringRef name, ArrayRef data); MOCKABLE_VIRTUAL void dumpKernelData(ConstStringRef name, ArrayRef data); MOCKABLE_VIRTUAL void dumpRel(ConstStringRef name, ArrayRef data); MOCKABLE_VIRTUAL void dumpRela(ConstStringRef name, ArrayRef data); MOCKABLE_VIRTUAL void dumpSymtab(ElfT &elf, ArrayRef symtabData); MOCKABLE_VIRTUAL std::vector dumpElfSections(ElfT &elf); MOCKABLE_VIRTUAL void dumpSectionInfo(const std::vector §ionInfos); MOCKABLE_VIRTUAL std::vector getIntelGTNotes(ElfT &elf); public: bool &showHelp = arguments.showHelp; protected: Arguments arguments; OclocArgHelper *argHelper; std::unique_ptr iga; }; template class ZebinEncoder { public: using ElfEncoderT = Elf::ElfEncoder; using SecNameToIdMapT = std::unordered_map; using ElfSecHdrT = Elf::ElfSectionHeader; using ElfRelT = Elf::ElfRel; using ElfRelaT = Elf::ElfRela; using ElfSymT = Elf::ElfSymbolEntry; ZebinEncoder(OclocArgHelper *argHelper); ~ZebinEncoder(); ErrorCode encode(); ErrorCode validateInput(const std::vector &args); void printHelp(); protected: MOCKABLE_VIRTUAL ErrorCode loadSectionsInfo(std::vector §ionInfos); MOCKABLE_VIRTUAL ErrorCode checkIfAllFilesExist(const std::vector §ionInfos); MOCKABLE_VIRTUAL std::vector getIntelGTNotesSection(const std::vector §ionInfos); MOCKABLE_VIRTUAL std::vector getIntelGTNotes(const std::vector &intelGtNotesSection); MOCKABLE_VIRTUAL ErrorCode appendSections(ElfEncoderT &encoder, const std::vector §ionInfos); MOCKABLE_VIRTUAL ErrorCode appendRel(ElfEncoderT &encoder, const SectionInfo §ion, size_t targetSecId, size_t symtabSecId); MOCKABLE_VIRTUAL ErrorCode appendRela(ElfEncoderT &encoder, const SectionInfo §ion, size_t targetSecId, size_t symtabSecId); MOCKABLE_VIRTUAL ErrorCode appendKernel(ElfEncoderT &encoder, const SectionInfo §ion); MOCKABLE_VIRTUAL ErrorCode appendSymtab(ElfEncoderT &encoder, const SectionInfo §ion, size_t strtabId, SecNameToIdMapT secNameToId); MOCKABLE_VIRTUAL ErrorCode appendOther(ElfEncoderT &encoder, const SectionInfo §ion); MOCKABLE_VIRTUAL std::string getFilePath(const std::string &filename); MOCKABLE_VIRTUAL std::string parseKernelAssembly(ArrayRef kernelAssembly); MOCKABLE_VIRTUAL std::vector parseLine(const std::string &line); MOCKABLE_VIRTUAL std::vector parseRel(const std::vector &relocationsFile); MOCKABLE_VIRTUAL std::vector parseRela(const std::vector &relocationsFile); MOCKABLE_VIRTUAL std::vector parseSymbols(const std::vector &symbolsFile, ElfEncoderT &encoder, size_t &outNumLocalSymbols, SecNameToIdMapT secNameToId); public: bool &showHelp = arguments.showHelp; protected: Arguments arguments; OclocArgHelper *argHelper; std::unique_ptr iga; }; }; // namespace Zebin::Manipulator } // namespace NEO