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

Last change on this file since 2510 was 2510, checked in by matszpk, 4 years ago

CLRadeonExtender: Remove obsolete "#include <map>".

File size: 5.0 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 <cstdint>
22#include <utility>
23#include <CLRX/amdbin/Elf.h>
24#include <CLRX/utils/Utilities.h>
25#include <CLRX/utils/MemAccess.h>
26#include <CLRX/amdbin/ROCmBinaries.h>
27
28using namespace CLRX;
29
30ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags)
31        : ElfBinary64(binaryCodeSize, binaryCode, creationFlags),
32          kernelsNum(0), codeSize(0), code(nullptr)
33{
34    cxuint textIndex = SHN_UNDEF;
35    try
36    { textIndex = getSectionIndex(".text"); }
37    catch(const Exception& ex)
38    { } // ignore failed
39    uint64_t codeOffset = 0;
40    if (textIndex!=SHN_UNDEF)
41    {
42        code = getSectionContent(textIndex);
43        const Elf64_Shdr& textShdr = getSectionHeader(textIndex);
44        codeSize = ULEV(textShdr.sh_size);
45        codeOffset = ULEV(textShdr.sh_offset);
46    }
47   
48    kernelsNum = 0;
49    const size_t symbolsNum = getSymbolsNum();
50    for (size_t i = 0; i < symbolsNum; i++)
51    {
52        const Elf64_Sym& sym = getSymbol(i);
53        if (sym.st_shndx==textIndex)
54            kernelsNum++;
55    }
56    if (code==nullptr && kernelsNum!=0)
57        throw Exception("No code if kernels number is not zero");
58    kernels.reset(new ROCmKernel[kernelsNum]);
59    size_t j = 0;
60   
61    typedef std::pair<uint64_t, size_t> KernelOffsetEntry;
62    std::unique_ptr<KernelOffsetEntry[]> kernelOffsets(new KernelOffsetEntry[kernelsNum]);
63    for (size_t i = 0; i < symbolsNum; i++)
64    {
65        const Elf64_Sym& sym = getSymbol(i);
66        if (sym.st_shndx!=textIndex)
67            continue;
68        const size_t value = ULEV(sym.st_value);
69        if (value < codeOffset)
70            throw Exception("Kernel offset is too small!");
71        const size_t size = ULEV(sym.st_size);
72        if (value+0x100 > codeOffset+codeSize)
73            throw Exception("Kernel offset is too big!");
74        kernelOffsets[j] = std::make_pair(value, j);
75        kernels[j++] = { getSymbolName(i), value, size, 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 > codeOffset+codeSize)
94            throw Exception("Kernel size is too small!");
95        ROCmKernel& kernel = kernels[kernelOffsets[kernelsNum-1].second];
96        uint64_t kcodeSize = codeSize -
97                (kernelOffsets[kernelsNum-1].first+0x100-codeOffset);
98        if (kernel.codeSize==0 && kcodeSize>0)
99            kernel.codeSize = kcodeSize;
100        if (kernel.codeSize > kcodeSize)
101            throw Exception("Kernel code size out of range");
102    }
103   
104    if (hasKernelMap())
105    {   // create kernels map
106        kernelsMap.resize(kernelsNum);
107        for (size_t i = 0; i < kernelsNum; i++)
108            kernelsMap[i] = std::make_pair(kernels[i].kernelName, i);
109        mapSort(kernelsMap.begin(), kernelsMap.end());
110    }
111}
112
113const ROCmKernel& ROCmBinary::getKernel(const char* name) const
114{
115    KernelMap::const_iterator it = binaryMapFind(kernelsMap.begin(),
116                             kernelsMap.end(), name);
117    if (it == kernelsMap.end())
118        throw Exception("Can't find kernel name");
119    return kernels[it->second];
120}
121
122bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary)
123{
124    if (!isElfBinary(binarySize, binary))
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.