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

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

CLRadeonExtender: Ignore non-global visibility for kernel symbols.

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        if ((symType==STT_GNU_IFUNC || symType==STT_OBJECT))
77        {
78            const bool isKernel = (symType==STT_GNU_IFUNC);
79            symOffsets[j] = std::make_pair(value, j);
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    }
85    std::sort(symOffsets.get(), symOffsets.get()+regionsNum,
86            [](const RegionOffsetEntry& a, const RegionOffsetEntry& b)
87            { return a.first < b.first; });
88    // checking distance between regions
89    for (size_t i = 1; i <= regionsNum; i++)
90    {
91        size_t end = (i<regionsNum) ? symOffsets[i].first : codeOffset+codeSize;
92        ROCmRegion& region = regions[symOffsets[i-1].second];
93        if (region.isKernel && symOffsets[i-1].first+0x100 > end)
94            throw Exception("Kernel size is too small!");
95       
96        const uint64_t regSize = end - symOffsets[i-1].first;
97        if (region.size==0)
98            region.size = regSize;
99        else
100            region.size = std::min(regSize, region.size);
101    }
102   
103    if (hasRegionMap())
104    {   // create region map
105        regionsMap.resize(regionsNum);
106        for (size_t i = 0; i < regionsNum; i++)
107            regionsMap[i] = std::make_pair(regions[i].regionName, i);
108        mapSort(regionsMap.begin(), regionsMap.end());
109    }
110}
111
112const ROCmRegion& ROCmBinary::getRegion(const char* name) const
113{
114    RegionMap::const_iterator it = binaryMapFind(regionsMap.begin(),
115                             regionsMap.end(), name);
116    if (it == regionsMap.end())
117        throw Exception("Can't find region name");
118    return regions[it->second];
119}
120
121bool CLRX::isROCmBinary(size_t binarySize, const cxbyte* binary)
122{
123    if (!isElfBinary(binarySize, binary))
124        return false;
125    if (binary[EI_CLASS] != ELFCLASS64)
126        return false;
127    const Elf64_Ehdr* ehdr = reinterpret_cast<const Elf64_Ehdr*>(binary);
128    if (ULEV(ehdr->e_machine) != 0xe0 || ULEV(ehdr->e_flags)!=0)
129        return false;
130    return true;
131}
Note: See TracBrowser for help on using the repository browser.