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

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

CLRadeonExtender: Store kernels and regions in region list together (instead of same kernels).

File size: 4.8 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          regionsNum(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    regionsNum = 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        const cxbyte symType = ELF64_ST_TYPE(sym.st_info);
54        if (sym.st_shndx==textIndex &&
55            (symType==STT_GNU_IFUNC || symType==STT_OBJECT))
56            regionsNum++;
57    }
58    if (code==nullptr && regionsNum!=0)
59        throw Exception("No code if regions number is not zero");
60    regions.reset(new ROCmRegion[regionsNum]);
61    size_t j = 0;
62    typedef std::pair<uint64_t, size_t> RegionOffsetEntry;
63    std::unique_ptr<RegionOffsetEntry[]> symOffsets(new RegionOffsetEntry[regionsNum]);
64   
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        if (value < codeOffset)
72            throw Exception("Region offset is too small!");
73        const size_t size = ULEV(sym.st_size);
74       
75        const cxbyte symType = ELF64_ST_TYPE(sym.st_info);
76        const bool isKernel = (symType==STT_GNU_IFUNC);
77        if (symType==STT_GNU_IFUNC || symType==STT_OBJECT)
78            symOffsets[j] = std::make_pair(value, j);
79       
80        if (isKernel && value+0x100 > codeOffset+codeSize)
81            throw Exception("Kernel offset is too big!");
82        regions[j++] = { getSymbolName(i), size, value, isKernel };
83    }
84    std::sort(symOffsets.get(), symOffsets.get()+regionsNum,
85            [](const RegionOffsetEntry& a, const RegionOffsetEntry& b)
86            { return a.first < b.first; });
87    // checking distance between regions
88    for (size_t i = 1; i <= regionsNum; i++)
89    {
90        size_t end = (i<regionsNum) ? symOffsets[i].first : codeOffset+codeSize;
91        ROCmRegion& region = regions[symOffsets[i-1].second];
92        if (region.isKernel && symOffsets[i-1].first+0x100 > end)
93            throw Exception("Kernel size is too small!");
94       
95        const uint64_t regSize = end - symOffsets[i-1].first;
96        if (region.size==0)
97            region.size = regSize;
98        else
99            region.size = std::min(regSize, region.size);
100    }
101   
102    if (hasRegionMap())
103    {   // create region map
104        regionsMap.resize(regionsNum);
105        for (size_t i = 0; i < regionsNum; i++)
106            regionsMap[i] = std::make_pair(regions[i].regionName, i);
107        mapSort(regionsMap.begin(), regionsMap.end());
108    }
109}
110
111const ROCmRegion& ROCmBinary::getRegion(const char* name) const
112{
113    RegionMap::const_iterator it = binaryMapFind(regionsMap.begin(),
114                             regionsMap.end(), name);
115    if (it == regionsMap.end())
116        throw Exception("Can't find region name");
117    return regions[it->second];
118}
119
120bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary)
121{
122    if (!isElfBinary(binarySize, binary))
123        return false;
124    if (binary[EI_CLASS] != ELFCLASS64)
125        return false;
126    const Elf64_Ehdr* ehdr = reinterpret_cast<const Elf64_Ehdr*>(binary);
127    if (ULEV(ehdr->e_machine) != 0xe0 || ULEV(ehdr->e_flags)!=0)
128        return false;
129    return true;
130}
Note: See TracBrowser for help on using the repository browser.