Changeset 3763 in CLRX


Ignore:
Timestamp:
Feb 9, 2018, 7:30:28 PM (15 months ago)
Author:
matszpk
Message:

CLRadeonExtender: Prepping ROCmBinGenerator to getting section's offsets.

Location:
CLRadeonExtender/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • CLRadeonExtender/trunk/CLRX/amdbin/ROCmBinaries.h

    r3726 r3763  
    389389    bool manageable;
    390390    const ROCmInput* input;
     391    std::unique_ptr<ElfBinaryGen64> elfBinGen64;
     392    size_t binarySize;
     393    size_t commentSize;
     394    const char* comment;
     395    std::string target;
     396    std::unique_ptr<cxbyte[]> noteBuf;
     397    std::string metadataStr;
     398    size_t metadataSize;
     399    const char* metadata;
     400    uint16_t mainBuiltinSectTable[ROCMSECTID_MAX-ELFSECTID_START+1];
    391401   
    392402    void generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
    393              Array<cxbyte>* aPtr) const;
     403             Array<cxbyte>* aPtr);
    394404public:
    395405    /// constructor
     
    425435    { return input; }
    426436   
     437    // prepare binary generator (for section diffs)
     438    void prepareBinaryGen();
     439   
     440    size_t getSectionOffset(cxuint sectionId) const
     441    { return elfBinGen64->getRegionOffset(mainBuiltinSectTable[sectionId]); }
     442   
    427443    /// set input
    428444    void setInput(const ROCmInput* input);
    429445   
    430446    /// generates binary to array of bytes
    431     void generate(Array<cxbyte>& array) const;
     447    void generate(Array<cxbyte>& array);
    432448   
    433449    /// generates binary to output stream
    434     void generate(std::ostream& os) const;
     450    void generate(std::ostream& os);
    435451   
    436452    /// generates binary to vector of char
    437     void generate(std::vector<char>& vector) const;
     453    void generate(std::vector<char>& vector);
    438454};
    439455
  • CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp

    r3761 r3763  
    20062006// TODO: add GLOBAL OFFSET TABLE dynamic and (static?) relocations
    20072007
    2008 void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
    2009              Array<cxbyte>* aPtr) const
     2008void ROCmBinGenerator::prepareBinaryGen()
    20102009{
    20112010    AMDGPUArchVersion amdGpuArchValues = getGPUArchVersion(input->deviceType,
     
    20162015        amdGpuArchValues.stepping = input->archStepping;
    20172016   
    2018     const char* comment = "CLRX ROCmBinGenerator " CLRX_VERSION;
    2019     uint32_t commentSize = ::strlen(comment);
     2017    comment = "CLRX ROCmBinGenerator " CLRX_VERSION;
     2018    commentSize = ::strlen(comment);
    20202019    if (input->comment!=nullptr)
    20212020    {
     
    20312030        eflags = input->eflags;
    20322031   
    2033     ElfBinaryGen64 elfBinGen64({ 0U, 0U, 0x40, 0, ET_DYN,
     2032    elfBinGen64.reset(new ElfBinaryGen64({ 0U, 0U, 0x40, 0, ET_DYN,
    20342033            0xe0, EV_CURRENT, UINT_MAX, 0, eflags },
    2035             true, true, true, PHREGION_FILESTART);
    2036    
    2037     uint16_t mainBuiltinSectTable[ROCMSECTID_MAX-ELFSECTID_START+1];
     2034            true, true, true, PHREGION_FILESTART));
     2035   
    20382036    std::fill(mainBuiltinSectTable,
    20392037              mainBuiltinSectTable + ROCMSECTID_MAX-ELFSECTID_START+1, SHN_UNDEF);
     
    20622060   
    20632061    // add symbols (kernels, function kernels and data symbols)
    2064     elfBinGen64.addSymbol(ElfSymbol64("_DYNAMIC",
     2062    elfBinGen64->addSymbol(ElfSymbol64("_DYNAMIC",
    20652063                  mainBuiltinSectTable[ROCMSECTID_DYNAMIC-ELFSECTID_START],
    20662064                  ELF64_ST_INFO(STB_LOCAL, STT_NOTYPE), STV_HIDDEN, true, 0, 0));
     
    20902088        }
    20912089        // add to symbols and dynamic symbols table
    2092         elfBinGen64.addSymbol(elfsym);
    2093         elfBinGen64.addDynSymbol(elfsym);
     2090        elfBinGen64->addSymbol(elfsym);
     2091        elfBinGen64->addDynSymbol(elfsym);
    20942092    }
    20952093   
    20962094    static const int32_t dynTags[] = {
    20972095        DT_SYMTAB, DT_SYMENT, DT_STRTAB, DT_STRSZ, DT_HASH };
    2098     elfBinGen64.addDynamics(sizeof(dynTags)/sizeof(int32_t), dynTags);
     2096    elfBinGen64->addDynamics(sizeof(dynTags)/sizeof(int32_t), dynTags);
    20992097   
    21002098    // elf program headers
    2101     elfBinGen64.addProgramHeader({ PT_PHDR, PF_R, 0, 1,
     2099    elfBinGen64->addProgramHeader({ PT_PHDR, PF_R, 0, 1,
    21022100                    true, Elf64Types::nobase, Elf64Types::nobase, 0 });
    2103     elfBinGen64.addProgramHeader({ PT_LOAD, PF_R, PHREGION_FILESTART,
     2101    elfBinGen64->addProgramHeader({ PT_LOAD, PF_R, PHREGION_FILESTART,
    21042102                    execProgHeaderRegionIndex,
    21052103                    true, Elf64Types::nobase, Elf64Types::nobase, 0, 0x1000 });
    2106     elfBinGen64.addProgramHeader({ PT_LOAD, PF_R|PF_X, execProgHeaderRegionIndex, 1,
     2104    elfBinGen64->addProgramHeader({ PT_LOAD, PF_R|PF_X, execProgHeaderRegionIndex, 1,
    21072105                    true, Elf64Types::nobase, Elf64Types::nobase, 0 });
    2108     elfBinGen64.addProgramHeader({ PT_LOAD, PF_R|PF_W, execProgHeaderRegionIndex+1, 1,
     2106    elfBinGen64->addProgramHeader({ PT_LOAD, PF_R|PF_W, execProgHeaderRegionIndex+1, 1,
    21092107                    true, Elf64Types::nobase, Elf64Types::nobase, 0 });
    2110     elfBinGen64.addProgramHeader({ PT_DYNAMIC, PF_R|PF_W, execProgHeaderRegionIndex+1, 1,
     2108    elfBinGen64->addProgramHeader({ PT_DYNAMIC, PF_R|PF_W, execProgHeaderRegionIndex+1, 1,
    21112109                    true, Elf64Types::nobase, Elf64Types::nobase, 0, 8 });
    2112     elfBinGen64.addProgramHeader({ PT_GNU_RELRO, PF_R, execProgHeaderRegionIndex+1, 1,
     2110    elfBinGen64->addProgramHeader({ PT_GNU_RELRO, PF_R, execProgHeaderRegionIndex+1, 1,
    21132111                    true, Elf64Types::nobase, Elf64Types::nobase, 0, 1 });
    2114     elfBinGen64.addProgramHeader({ PT_GNU_STACK, PF_R|PF_W, PHREGION_FILESTART, 0,
     2112    elfBinGen64->addProgramHeader({ PT_GNU_STACK, PF_R|PF_W, PHREGION_FILESTART, 0,
    21152113                    true, 0, 0, 0 });
    21162114   
    21172115    if (input->newBinFormat)
    21182116        // program header for note (new binary format)
    2119         elfBinGen64.addProgramHeader({ PT_NOTE, PF_R, 1, 1, true,
     2117        elfBinGen64->addProgramHeader({ PT_NOTE, PF_R, 1, 1, true,
    21202118                    Elf64Types::nobase, Elf64Types::nobase, 0, 4 });
    21212119   
    2122     std::string target = input->target.c_str();
     2120    target = input->target.c_str();
    21232121    if (target.empty() && !input->targetTripple.empty())
    21242122    {
     
    21302128    }
    21312129    // elf notes
    2132     elfBinGen64.addNote({"AMD", sizeof noteDescType1, noteDescType1, 1U});
    2133     std::unique_ptr<cxbyte[]> noteBuf(new cxbyte[0x1b]);
     2130    elfBinGen64->addNote({"AMD", sizeof noteDescType1, noteDescType1, 1U});
     2131    noteBuf.reset(new cxbyte[0x1b]);
    21342132    ::memcpy(noteBuf.get(), noteDescType3, 0x1b);
    21352133    SULEV(*(uint32_t*)(noteBuf.get()+4), amdGpuArchValues.major);
    21362134    SULEV(*(uint32_t*)(noteBuf.get()+8), amdGpuArchValues.minor);
    21372135    SULEV(*(uint32_t*)(noteBuf.get()+12), amdGpuArchValues.stepping);
    2138     elfBinGen64.addNote({"AMD", 0x1b, noteBuf.get(), 3U});
     2136    elfBinGen64->addNote({"AMD", 0x1b, noteBuf.get(), 3U});
    21392137    if (!target.empty())
    2140         elfBinGen64.addNote({"AMD", target.size(), (const cxbyte*)target.c_str(), 0xbU});
    2141    
    2142     std::string metadataStr;
    2143     size_t metadataSize = input->metadataSize;
    2144     const char* metadata = input->metadata;
     2138        elfBinGen64->addNote({"AMD", target.size(), (const cxbyte*)target.c_str(), 0xbU});
     2139   
     2140    metadataSize = input->metadataSize;
     2141    metadata = input->metadata;
    21452142    if (input->useMetadataInfo)
    21462143    {
     
    21742171   
    21752172    if (metadataSize != 0)
    2176         elfBinGen64.addNote({"AMD", metadataSize, (const cxbyte*)metadata, 0xaU});
     2173        elfBinGen64->addNote({"AMD", metadataSize, (const cxbyte*)metadata, 0xaU});
    21772174   
    21782175    /// region and sections
    2179     elfBinGen64.addRegion(ElfRegion64::programHeaderTable());
     2176    elfBinGen64->addRegion(ElfRegion64::programHeaderTable());
    21802177    if (input->newBinFormat)
    2181         elfBinGen64.addRegion(ElfRegion64::noteSection());
     2178        elfBinGen64->addRegion(ElfRegion64::noteSection());
    21822179    if (input->globalData != nullptr)
    2183         elfBinGen64.addRegion(ElfRegion64(input->globalDataSize, input->globalData, 4,
     2180        elfBinGen64->addRegion(ElfRegion64(input->globalDataSize, input->globalData, 4,
    21842181                ".rodata", SHT_PROGBITS, SHF_ALLOC, 0, 0, Elf64Types::nobase));
    21852182   
    2186     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
     2183    elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
    21872184                ".dynsym", SHT_DYNSYM, SHF_ALLOC, 0, BINGEN_DEFAULT, Elf64Types::nobase));
    2188     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 4,
     2185    elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 4,
    21892186                ".hash", SHT_HASH, SHF_ALLOC,
    21902187                mainBuiltinSectTable[ELFSECTID_DYNSYM-ELFSECTID_START], 0,
    21912188                Elf64Types::nobase));
    2192     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB,
     2189    elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB,
    21932190                SHF_ALLOC, 0, 0, Elf64Types::nobase));
    21942191    // '.text' with alignment=4096
    2195     elfBinGen64.addRegion(ElfRegion64(input->codeSize, (const cxbyte*)input->code,
     2192    elfBinGen64->addRegion(ElfRegion64(input->codeSize, (const cxbyte*)input->code,
    21962193              0x1000, ".text", SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 0, 0,
    21972194              Elf64Types::nobase, 0, false, 256));
    2198     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 0x1000,
     2195    elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 0x1000,
    21992196                ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE,
    22002197                mainBuiltinSectTable[ELFSECTID_DYNSTR-ELFSECTID_START], 0,
     
    22022199    if (!input->newBinFormat)
    22032200    {
    2204         elfBinGen64.addRegion(ElfRegion64::noteSection());
    2205         elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1,
     2201        elfBinGen64->addRegion(ElfRegion64::noteSection());
     2202        elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1,
    22062203                    ".AMDGPU.config", SHT_PROGBITS, 0));
    22072204    }
    2208     elfBinGen64.addRegion(ElfRegion64(commentSize, (const cxbyte*)comment, 1, ".comment",
     2205    elfBinGen64->addRegion(ElfRegion64(commentSize, (const cxbyte*)comment, 1, ".comment",
    22092206              SHT_PROGBITS, SHF_MERGE|SHF_STRINGS, 0, 0, 0, 1));
    2210     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
     2207    elfBinGen64->addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
    22112208                ".symtab", SHT_SYMTAB, 0, 0, BINGEN_DEFAULT));
    2212     elfBinGen64.addRegion(ElfRegion64::shstrtabSection());
    2213     elfBinGen64.addRegion(ElfRegion64::strtabSection());
    2214     elfBinGen64.addRegion(ElfRegion64::sectionHeaderTable());
     2209    elfBinGen64->addRegion(ElfRegion64::shstrtabSection());
     2210    elfBinGen64->addRegion(ElfRegion64::strtabSection());
     2211    elfBinGen64->addRegion(ElfRegion64::sectionHeaderTable());
    22152212   
    22162213    /* extra sections */
    22172214    for (const BinSection& section: input->extraSections)
    2218         elfBinGen64.addRegion(ElfRegion64(section, mainBuiltinSectTable,
     2215        elfBinGen64->addRegion(ElfRegion64(section, mainBuiltinSectTable,
    22192216                         ROCMSECTID_MAX, mainSectionsNum));
    22202217    /* extra symbols */
     
    22232220        ElfSymbol64 sym(symbol, mainBuiltinSectTable,
    22242221                         ROCMSECTID_MAX, mainSectionsNum);
    2225         elfBinGen64.addSymbol(sym);
    2226         elfBinGen64.addDynSymbol(sym);
    2227     }
    2228    
    2229     size_t binarySize = elfBinGen64.countSize();
     2222        elfBinGen64->addSymbol(sym);
     2223        elfBinGen64->addDynSymbol(sym);
     2224    }
     2225    binarySize = elfBinGen64->countSize();
     2226}
     2227
     2228void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
     2229             Array<cxbyte>* aPtr)
     2230{
     2231    prepareBinaryGen();
    22302232    /****
    22312233     * prepare for write binary to output
     
    22572259     ****/
    22582260    FastOutputBuffer bos(256, *os);
    2259     elfBinGen64.generate(bos);
     2261    elfBinGen64->generate(bos);
    22602262    assert(bos.getWritten() == binarySize);
    22612263    }
     
    22682270}
    22692271
    2270 void ROCmBinGenerator::generate(Array<cxbyte>& array) const
     2272void ROCmBinGenerator::generate(Array<cxbyte>& array)
    22712273{
    22722274    generateInternal(nullptr, nullptr, &array);
    22732275}
    22742276
    2275 void ROCmBinGenerator::generate(std::ostream& os) const
     2277void ROCmBinGenerator::generate(std::ostream& os)
    22762278{
    22772279    generateInternal(&os, nullptr, nullptr);
    22782280}
    22792281
    2280 void ROCmBinGenerator::generate(std::vector<char>& v) const
     2282void ROCmBinGenerator::generate(std::vector<char>& v)
    22812283{
    22822284    generateInternal(nullptr, &v, nullptr);
Note: See TracChangeset for help on using the changeset viewer.