File: ZEInfoReader.cpp

package info (click to toggle)
intel-graphics-compiler 1.0.12504.6-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 83,912 kB
  • sloc: cpp: 910,147; lisp: 202,655; ansic: 15,197; python: 4,025; yacc: 2,241; lex: 1,570; pascal: 244; sh: 104; makefile: 25
file content (102 lines) | stat: -rw-r--r-- 3,010 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*========================== begin_copyright_notice ============================

Copyright (C) 2020-2021 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "ZEInfoReader.h"

#include "Tester.hpp"
#include <ZEInfo.hpp>
#include <ZEinfoYAML.hpp>

#include <llvm/Object/ObjectFile.h>
#include <llvm/Object/ELFObjectFile.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/Error.h>

#include <fstream>
#include <iostream>
#include <string>
#include <system_error>

using namespace std;
using namespace zebin;

/// ---------------- ELF Object Reader ------------------------------------ ///

static void dumpZEInfo(std::unique_ptr<llvm::object::ObjectFile> object) {
    bool dump = false;
    for (auto sect : object->sections()) {
        llvm::StringRef name;
        sect.getName(name);

        if (name.compare(llvm::StringRef(".ze_info")))
            continue;

        llvm::StringRef content;
        sect.getContents(content);

        std::ofstream outfile;
        outfile.open("ze_info.dump", std::ios::out | std::ios::binary);
        outfile.write(content.data(), content.size());
        outfile.close();
        if (dump)
            std::cerr << "Given ELF object has more than one .ze_info section";
        dump = true;
    }
    if (!dump)
        std::cerr << "Given ELF object has no .ze_info section";
}


/// ---------------- Command line options --------------------------------- ///
static llvm::cl::opt<string> InputFilename(
    llvm::cl::Positional, llvm::cl::desc("<input file>"));

static llvm::cl::opt<bool> DumpZEInfo ("info",
    llvm::cl::desc("Dump .ze_info section into ze_info.dump file"));

static llvm::cl::opt<bool> RunTestZEInfo ("test-ze-info",
    llvm::cl::desc("Run static zeinfo generating tests, print the result to std output"));
/// ----------------------------------------------------------------------- ///

int zeinfo_reader_main(int argc, const char** argv) {
    llvm::cl::ParseCommandLineOptions(argc, argv);

    // run zeinfo generating tests
    // FIXME: This is just a static test, need to be enhanced
    if (RunTestZEInfo) {
        Tester::testZEInfoOutput();
        return 0;
    }

    // read input elf file
    llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileOrErr =
        llvm::MemoryBuffer::getFile(InputFilename);

    if (FileOrErr.getError()) {
        std::cerr << "Cannot open file " << InputFilename;
        return 1;
    }

    llvm::MemoryBufferRef inputRef(*FileOrErr.get());

    llvm::Expected<std::unique_ptr<llvm::object::ObjectFile>>
        ObjOrErr(llvm::object::ObjectFile::createELFObjectFile(inputRef));

    if (!ObjOrErr) {
        std::cerr << "Unrecognized input format. ELF is expected.";
        return 1;
    }

    std::unique_ptr<llvm::object::ObjectFile> obj = std::move(ObjOrErr.get());

    if (DumpZEInfo)
        dumpZEInfo(std::move(obj));

    return 0;
}