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

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

CLRadeonExtender: ROCm: change code and setup into codeOffset and setupOffset. fixed setting of code size.

File size: 5.1 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    uint64_t codeOffset = 0;
45    if (textIndex!=SHN_UNDEF)
46    {
47        code = getSectionContent(textIndex);
48        const Elf64_Shdr& textShdr = getSectionHeader(textIndex);
49        codeSize = ULEV(textShdr.sh_size);
50        codeOffset = ULEV(textShdr.sh_offset);
51    }
52   
53    kernelsNum = 0;
54    const size_t symbolsNum = getSymbolsNum();
55    for (size_t i = 0; i < symbolsNum; i++)
56    {
57        const Elf64_Sym& sym = getSymbol(i);
58        if (sym.st_shndx==textIndex)
59            kernelsNum++;
60    }
61    if (code==nullptr && kernelsNum!=0)
62        throw Exception("No code if kernels number is not zero");
63    kernels.reset(new ROCmKernel[kernelsNum]);
64    size_t j = 0;
65   
66    typedef std::pair<uint64_t, size_t> KernelOffsetEntry;
67    std::unique_ptr<KernelOffsetEntry[]> kernelOffsets(new KernelOffsetEntry[kernelsNum]);
68    for (size_t i = 0; i < symbolsNum; i++)
69    {
70        const Elf64_Sym& sym = getSymbol(i);
71        if (sym.st_shndx!=textIndex)
72            continue;
73        const size_t value = ULEV(sym.st_value);
74        if (value < codeOffset)
75            throw Exception("Kernel offset is too small!");
76        const size_t size = ULEV(sym.st_size);
77        if (value+0x100 > codeOffset+codeSize)
78            throw Exception("Kernel offset is too big!");
79        kernelOffsets[j] = std::make_pair(value, j);
80        kernels[j++] = { getSymbolName(i), value, size, value+0x100 };
81    }
82    std::sort(kernelOffsets.get(), kernelOffsets.get()+kernelsNum,
83            [](const KernelOffsetEntry& a, const KernelOffsetEntry& b)
84            { return a.first < b.first; });
85    // checking distance between kernels
86    for (size_t i = 1; i < kernelsNum; i++)
87    {
88        if (kernelOffsets[i-1].first+0x100 > kernelOffsets[i].first)
89            throw Exception("Kernel size is too small!");
90        ROCmKernel& kernel = kernels[kernelOffsets[i-1].second];
91        uint64_t kcodeSize = kernelOffsets[i].first - (kernelOffsets[i-1].first+0x100);
92        if (kernel.codeSize==0 && kcodeSize>0)
93            kernel.codeSize = kcodeSize;
94        if (kernel.codeSize > kcodeSize)
95            throw Exception("Kernel code size out of range");
96    }
97    {   // last kernel in position
98        if (kernelOffsets[kernelsNum-1].first+0x100 > codeOffset+codeSize)
99            throw Exception("Kernel size is too small!");
100        ROCmKernel& kernel = kernels[kernelOffsets[kernelsNum-1].second];
101        uint64_t kcodeSize = codeSize -
102                (kernelOffsets[kernelsNum-1].first+0x100-codeOffset);
103        if (kernel.codeSize==0 && kcodeSize>0)
104            kernel.codeSize = kcodeSize;
105        if (kernel.codeSize > kcodeSize)
106            throw Exception("Kernel code size out of range");
107    }
108   
109    if (hasKernelMap())
110    {   // create kernels map
111        kernelsMap.resize(kernelsNum);
112        for (size_t i = 0; i < kernelsNum; i++)
113            kernelsMap[i] = std::make_pair(kernels[i].kernelName, i);
114        mapSort(kernelsMap.begin(), kernelsMap.end());
115    }
116}
117
118const ROCmKernel& ROCmBinary::getKernel(const char* name) const
119{
120    KernelMap::const_iterator it = binaryMapFind(kernelsMap.begin(),
121                             kernelsMap.end(), name);
122    if (it == kernelsMap.end())
123        throw Exception("Can't find kernel name");
124    return kernels[it->second];
125}
126
127bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary)
128{
129    if (!isElfBinary(binarySize, binary))
130        return false;
131        return false;
132    if (binary[EI_CLASS] != ELFCLASS64)
133        return false;
134    const Elf64_Ehdr* ehdr = reinterpret_cast<const Elf64_Ehdr*>(binary);
135    if (ULEV(ehdr->e_machine) != 0xe0 || ULEV(ehdr->e_flags)!=0)
136        return false;
137    return true;
138}
Note: See TracBrowser for help on using the repository browser.