source: CLRX/CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp @ 2504

Last change on this file since 2504 was 2504, checked in by matszpk, 3 years ago

CLRadeonExtender: ROCm: add codeSize field to ROCmKernel. Checking and setting kernel sizes.

File size: 4.9 KB
Line 
1/*
2 *  CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3 *  Copyright (C) 2014-2016 Mateusz Szpakowski
4 *
5 *  This library is free software; you can redistribute it and/or
6 *  modify it under the terms of the GNU Lesser General Public
7 *  License as published by the Free Software Foundation; either
8 *  version 2.1 of the License, or (at your option) any later version.
9 *
10 *  This library is distributed in the hope that it will be useful,
11 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 *  Lesser General Public License for more details.
14 *
15 *  You should have received a copy of the GNU Lesser General Public
16 *  License along with this library; if not, write to the Free Software
17 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20#include <CLRX/Config.h>
21#include <cstdlib>
22#include <cstring>
23#include <climits>
24#include <cstdint>
25#include <map>
26#include <utility>
27#include <vector>
28#include <CLRX/amdbin/Elf.h>
29#include <CLRX/utils/Utilities.h>
30#include <CLRX/utils/MemAccess.h>
31#include <CLRX/amdbin/ROCmBinaries.h>
32
33using namespace CLRX;
34
35ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags)
36        : ElfBinary64(binaryCodeSize, binaryCode, creationFlags),
37          kernelsNum(0), codeSize(0), code(nullptr)
38{
39    cxuint textIndex = SHN_UNDEF;
40    try
41    { textIndex = getSectionIndex(".text"); }
42    catch(const Exception& ex)
43    { } // ignore failed
44    if (textIndex!=SHN_UNDEF)
45    {
46        code = getSectionContent(textIndex);
47        codeSize = ULEV(getSectionHeader(textIndex).sh_size);
48    }
49   
50    kernelsNum = 0;
51    const size_t symbolsNum = getSymbolsNum();
52    for (size_t i = 0; i < symbolsNum; i++)
53    {
54        const Elf64_Sym& sym = getSymbol(i);
55        if (sym.st_shndx==textIndex)
56            kernelsNum++;
57    }
58    if (code==nullptr && kernelsNum!=0)
59        throw Exception("No code if kernels number is not zero");
60    kernels.reset(new ROCmKernel[kernelsNum]);
61    size_t j = 0;
62   
63    typedef std::pair<uint64_t, size_t> KernelOffsetEntry;
64    std::unique_ptr<KernelOffsetEntry[]> kernelOffsets(new KernelOffsetEntry[kernelsNum]);
65    for (size_t i = 0; i < symbolsNum; i++)
66    {
67        const Elf64_Sym& sym = getSymbol(i);
68        if (sym.st_shndx!=textIndex)
69            continue;
70        const size_t value = ULEV(sym.st_value);
71        const size_t size = ULEV(sym.st_size);
72        if (value+0x100 > codeSize)
73            throw Exception("Kernel offset is too big!");
74        kernelOffsets[j] = std::make_pair(value, j);
75        kernels[j++] = { getSymbolName(i), code+value, size, code+value+0x100 };
76    }
77    std::sort(kernelOffsets.get(), kernelOffsets.get()+kernelsNum,
78            [](const KernelOffsetEntry& a, const KernelOffsetEntry& b)
79            { return a.first < b.first; });
80    // checking distance between kernels
81    for (size_t i = 1; i < kernelsNum; i++)
82    {
83        if (kernelOffsets[i-1].first+0x100 > kernelOffsets[i].first)
84            throw Exception("Kernel size is too small!");
85        ROCmKernel& kernel = kernels[kernelOffsets[i-1].second];
86        uint64_t kcodeSize = kernelOffsets[i].first - (kernelOffsets[i-1].first+0x100);
87        if (kernel.codeSize==0 && kcodeSize>0)
88            kernel.codeSize = kcodeSize;
89        if (kernel.codeSize > kcodeSize)
90            throw Exception("Kernel code size out of range");
91    }
92    {   // last kernel in position
93        if (kernelOffsets[kernelsNum-1].first+0x100 > codeSize)
94            throw Exception("Kernel size is too small!");
95        ROCmKernel& kernel = kernels[kernelOffsets[kernelsNum-1].second];
96        uint64_t kcodeSize = codeSize - (kernelOffsets[kernelsNum-1].first+0x100);
97        if (kernel.codeSize==0 && kcodeSize>0)
98            kernel.codeSize = kcodeSize;
99        if (kernel.codeSize > kcodeSize)
100            throw Exception("Kernel code size out of range");
101    }
102   
103    if (hasKernelMap())
104    {   // create kernels map
105        kernelsMap.resize(kernelsNum);
106        for (size_t i = 0; i < kernelsNum; i++)
107            kernelsMap[i] = std::make_pair(kernels[i].kernelName, i);
108        mapSort(kernelsMap.begin(), kernelsMap.end());
109    }
110}
111
112const ROCmKernel& ROCmBinary::getKernel(const char* name) const
113{
114    KernelMap::const_iterator it = binaryMapFind(kernelsMap.begin(),
115                             kernelsMap.end(), name);
116    if (it == kernelsMap.end())
117        throw Exception("Can't find kernel name");
118    return kernels[it->second];
119}
120
121bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary)
122{
123    if (!isElfBinary(binarySize, binary))
124        return false;
125        return false;
126    if (binary[EI_CLASS] != ELFCLASS64)
127        return false;
128    const Elf64_Ehdr* ehdr = reinterpret_cast<const Elf64_Ehdr*>(binary);
129    if (ULEV(ehdr->e_machine) != 0xe0 || ULEV(ehdr->e_flags)!=0)
130        return false;
131    return true;
132}
Note: See TracBrowser for help on using the repository browser.