Ignore:
Timestamp:
Jan 27, 2018, 1:27:51 PM (23 months ago)
Author:
matszpk
Message:

CLRadeonExtender: DisasmROCm: print globalData (rodata) and newbinfmt (if new binary format).
ROCmBinGen: rewrite the creation of mainBuiltinSectionTable: create dynamically. Set eflags by default (if not given). Add globalData (if supplied) to binary.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • CLRadeonExtender/trunk/amdbin/ROCmBinaries.cpp

    r3665 r3666  
    3636ROCmBinary::ROCmBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags)
    3737        : ElfBinary64(binaryCodeSize, binaryCode, creationFlags),
    38           regionsNum(0), codeSize(0), code(nullptr), metadataSize(0), metadata(nullptr)
     38          regionsNum(0), codeSize(0), code(nullptr),
     39          globalDataSize(0), globalData(nullptr), metadataSize(0), metadata(nullptr),
     40          newBinFormat(false)
    3941{
    4042    cxuint textIndex = SHN_UNDEF;
     
    5254        codeOffset = ULEV(textShdr.sh_offset);
    5355    }
     56   
     57    cxuint rodataIndex = SHN_UNDEF;
     58    try
     59    { rodataIndex = getSectionIndex(".rodata"); }
     60    catch(const Exception& ex)
     61    { } // ignore failed
     62    // find '.text' section
     63    if (rodataIndex!=SHN_UNDEF)
     64    {
     65        globalData = getSectionContent(rodataIndex);
     66        const Elf64_Shdr& rodataShdr = getSectionHeader(rodataIndex);
     67        globalDataSize = ULEV(rodataShdr.sh_size);
     68    }
     69   
     70    cxuint gpuConfigIndex = SHN_UNDEF;
     71    try
     72    { gpuConfigIndex = getSectionIndex(".AMDGPU.config"); }
     73    catch(const Exception& ex)
     74    { } // ignore failed
     75    newBinFormat = (gpuConfigIndex == SHN_UNDEF);
    5476   
    5577    // counts regions (symbol or kernel)
     
    230252    symbols.push_back({ kernelName, 0, 0, ROCmRegionType::KERNEL });
    231253}
     254
    232255/*
    233256 * ROCm Binary Generator
     
    243266ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType,
    244267        uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code,
     268        size_t globalDataSize, const cxbyte* globalData,
    245269        const std::vector<ROCmSymbolInput>& symbols)
    246270{
    247271    input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false,
    248             symbols, codeSize, code };
     272            globalDataSize, globalData, symbols, codeSize, code };
    249273}
    250274
    251275ROCmBinGenerator::ROCmBinGenerator(GPUDeviceType deviceType,
    252276        uint32_t archMinor, uint32_t archStepping, size_t codeSize, const cxbyte* code,
     277        size_t globalDataSize, const cxbyte* globalData,
    253278        std::vector<ROCmSymbolInput>&& symbols)
    254279{
    255280    input = new ROCmInput{ deviceType, archMinor, archStepping, 0, false,
    256             std::move(symbols), codeSize, code };
     281            globalDataSize, globalData, std::move(symbols), codeSize, code };
    257282}
    258283
     
    279304  'A', 'M', 'D', 0, 'A', 'M', 'D', 'G', 'P', 'U', 0 };
    280305
    281 // section index for symbol binding
    282 static const uint16_t mainBuiltinSectionTable[] =
    283 {
    284     10, // ELFSECTID_SHSTRTAB
    285     11, // ELFSECTID_STRTAB
    286     9, // ELFSECTID_SYMTAB
    287     3, // ELFSECTID_DYNSTR
    288     1, // ELFSECTID_DYNSYM
    289     4, // ELFSECTID_TEXT
    290     SHN_UNDEF, // ELFSECTID_RODATA
    291     SHN_UNDEF, // ELFSECTID_DATA
    292     SHN_UNDEF, // ELFSECTID_BSS
    293     8, // ELFSECTID_COMMENT
    294     2, // ROCMSECTID_HASH
    295     5, // ROCMSECTID_DYNAMIC
    296     6, // ROCMSECTID_NOTE
    297     7 // ROCMSECTID_GPUCONFIG
    298 };
     306static inline void addMainSectionToTable(cxuint& sectionsNum, uint16_t* builtinTable,
     307                cxuint elfSectId)
     308{ builtinTable[elfSectId - ELFSECTID_START] = sectionsNum++; }
    299309
    300310void ROCmBinGenerator::generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
     
    319329    }
    320330   
     331    uint32_t eflags = input->newBinFormat ? 2 : 0;
     332    if (input->eflags != BINGEN_DEFAULT)
     333        eflags = input->eflags;
     334   
    321335    ElfBinaryGen64 elfBinGen64({ 0U, 0U, 0x40, 0, ET_DYN,
    322             0xe0, EV_CURRENT, UINT_MAX, 0, input->eflags },
     336            0xe0, EV_CURRENT, UINT_MAX, 0, eflags },
    323337            true, true, true, PHREGION_FILESTART);
    324338    // add symbols (kernels, function kernels and data symbols)
     
    356370        DT_SYMTAB, DT_SYMENT, DT_STRTAB, DT_STRSZ, DT_HASH };
    357371    elfBinGen64.addDynamics(sizeof(dynTags)/sizeof(int32_t), dynTags);
     372   
     373    uint16_t mainBuiltinSectTable[ROCMSECTID_MAX-ELFSECTID_START+1];
     374    std::fill(mainBuiltinSectTable,
     375              mainBuiltinSectTable + ROCMSECTID_MAX-ELFSECTID_START+1, SHN_UNDEF);
     376    cxuint mainSectionsNum = 1;
     377   
     378    // generate main builtin section table (for section id translation)
     379    if (input->newBinFormat)
     380        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE);
     381    if (input->globalData != nullptr)
     382        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_RODATA);
     383    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSYM);
     384    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_HASH);
     385    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_DYNSTR);
     386    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_TEXT);
     387    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_DYNAMIC);
     388    if (!input->newBinFormat)
     389    {
     390        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_NOTE);
     391        addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ROCMSECTID_GPUCONFIG);
     392    }
     393    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_COMMENT);
     394    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SYMTAB);
     395    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_SHSTRTAB);
     396    addMainSectionToTable(mainSectionsNum, mainBuiltinSectTable, ELFSECTID_STRTAB);
     397   
    358398    // elf program headers
    359399    elfBinGen64.addProgramHeader({ PT_PHDR, PF_R, 0, 1,
     
    389429    /// region and sections
    390430    elfBinGen64.addRegion(ElfRegion64::programHeaderTable());
     431    if (input->newBinFormat)
     432        elfBinGen64.addRegion(ElfRegion64::noteSection());
     433    if (input->globalData != nullptr)
     434        elfBinGen64.addRegion(ElfRegion64(input->globalDataSize, input->globalData, 4,
     435                ".rodata", SHT_PROGBITS, SHF_ALLOC, 0, 0, Elf64Types::nobase));
     436   
    391437    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 8,
    392438                ".dynsym", SHT_DYNSYM, SHF_ALLOC, 0, 1, Elf64Types::nobase));
    393439    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 4,
    394                 ".hash", SHT_HASH, SHF_ALLOC, 1, 0, Elf64Types::nobase));
     440                ".hash", SHT_HASH, SHF_ALLOC,
     441                mainBuiltinSectTable[ELFSECTID_DYNSYM-ELFSECTID_START], 0,
     442                Elf64Types::nobase));
    395443    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1, ".dynstr", SHT_STRTAB,
    396444                SHF_ALLOC, 0, 0, Elf64Types::nobase));
     
    400448              Elf64Types::nobase, 0, false, 256));
    401449    elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 0x1000,
    402                 ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE, 3, 0,
     450                ".dynamic", SHT_DYNAMIC, SHF_ALLOC|SHF_WRITE,
     451                mainBuiltinSectTable[ELFSECTID_DYNSTR-ELFSECTID_START], 0,
    403452                Elf64Types::nobase, 0, false, 8));
    404     elfBinGen64.addRegion(ElfRegion64::noteSection());
    405     elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1,
    406                 ".AMDGPU.config", SHT_PROGBITS, 0));
     453    if (!input->newBinFormat)
     454    {
     455        elfBinGen64.addRegion(ElfRegion64::noteSection());
     456        elfBinGen64.addRegion(ElfRegion64(0, (const cxbyte*)nullptr, 1,
     457                    ".AMDGPU.config", SHT_PROGBITS, 0));
     458    }
    407459    elfBinGen64.addRegion(ElfRegion64(commentSize, (const cxbyte*)comment, 1, ".comment",
    408460              SHT_PROGBITS, SHF_MERGE|SHF_STRINGS, 0, 0, 0, 1));
     
    415467    /* extra sections */
    416468    for (const BinSection& section: input->extraSections)
    417         elfBinGen64.addRegion(ElfRegion64(section, mainBuiltinSectionTable,
    418                          ROCMSECTID_MAX, 12));
     469        elfBinGen64.addRegion(ElfRegion64(section, mainBuiltinSectTable,
     470                         ROCMSECTID_MAX, mainSectionsNum));
    419471    /* extra symbols */
    420472    for (const BinSymbol& symbol: input->extraSymbols)
    421         elfBinGen64.addSymbol(ElfSymbol64(symbol, mainBuiltinSectionTable,
    422                          ROCMSECTID_MAX, 12));
     473        elfBinGen64.addSymbol(ElfSymbol64(symbol, mainBuiltinSectTable,
     474                         ROCMSECTID_MAX, mainSectionsNum));
    423475   
    424476    size_t binarySize = elfBinGen64.countSize();
Note: See TracChangeset for help on using the changeset viewer.