Example #1
0
void BFIPatcher::readELFSections() {
    ELFHeader *header;
    ELFSectionHeader *sectionHeader;
    ELFSectionHeader *rawSectionHeader;
    uint64_t sectionStartOffset, sectionNameOffset;
    uint32_t sectionId;
    
    SectionInfo SectionInformation;
    
    printf("Reading ELF header...\n");
    
    header = (ELFHeader *) &this->unpatchedBinary[this->innerELFOffset];
    printElfHeader(header);
    
    if (header->ident1 != 0x64010101464c457f) {
        printf("Fatal error: ELF ident1 invalid!\n");
        exit(1);
    }
    
    if (header->shoff == 0) {
        printf("Fatal error: ELF shoff = 0!\n");
        exit(1);
    }
    
    if (header->shentsize != sizeof(ELFSectionHeader)) {
        printf("Fatal error: Section Header Entry Size mismatch!\n");
        exit(1);
    }
    
    sectionHeader = (ELFSectionHeader *) &this->unpatchedBinary[
            this->innerELFOffset + (header->shoff + (header->shstrndx * header->shentsize))];
    printElfSectionHeader(sectionHeader);
    
    // Get the section start offset.
    sectionStartOffset = this->innerELFOffset + header->shoff;
    
    for (sectionId = 0; sectionId < header->shnum; sectionId++) {
        printf("Section ID %d\n", sectionId);
        rawSectionHeader = (ELFSectionHeader *) &this->unpatchedBinary[sectionStartOffset + sectionId * header->shentsize];
        printElfSectionHeader(rawSectionHeader);
        printf("Text: %s\n\n", (char *)&this->unpatchedBinary[this->innerELFOffset + sectionHeader->offset + rawSectionHeader->nameIdx]);
        SectionInformation.name.clear();
        sectionNameOffset = this->innerELFOffset + sectionHeader->offset + rawSectionHeader->nameIdx;
        while (this->unpatchedBinary[sectionNameOffset]) {
            SectionInformation.name += (char) this->unpatchedBinary[sectionNameOffset];
            sectionNameOffset++;
        }
        SectionInformation.offset = rawSectionHeader->offset;
        SectionInformation.size = rawSectionHeader->size;
        this->ElfSections.push_back(SectionInformation);
    }
}
Example #2
0
void BFIPatcher::readELFSections() {
    BFIELFHeader *header;
    BFIELFSectionHeader *sectionHeader;
    BFIELFSectionHeader *rawSectionHeader;
    uint64_t sectionStartOffset, sectionNameOffset;
    uint32_t sectionId;
    
    SectionInfo SectionInformation;
    
    
    header = (BFIELFHeader *) &this->OpenCLBinary[this->innerELFOffset];
    
#if BFI_DEBUG
    bfi_printf("Reading ELF header...\n");
    printElfHeader(header);
#endif
    
    // Check the first 8 bytes - if this is not correct, we have not found an ELF.
    // A gnome, perhaps.
    if (header->ident1 != 0x64010101464c457f) {
        printf("Fatal error: ELF ident1 incorrect!\n");
        exit(1);
    }
    
    // This is being checked in the Python so I check it here!
    if (header->shoff == 0) {
        printf("Fatal error: ELF shoff = 0!\n");
        exit(1);
    }
    
    // If the section header entry size does not match our struct, something is
    // quite wrong and we should exit.
    if (header->shentsize != sizeof(BFIELFSectionHeader)) {
        printf("Fatal error: Section Header Entry Size mismatch!\n");
        exit(1);
    }
    
    // Read the first section header for the text offset.
    sectionHeader = (BFIELFSectionHeader *) &this->OpenCLBinary[
            this->innerELFOffset + (header->shoff + (header->shstrndx * header->shentsize))];
    printElfSectionHeader(sectionHeader);
    
    // Get the section start offset.
    sectionStartOffset = this->innerELFOffset + header->shoff;
    
    for (sectionId = 0; sectionId < header->shnum; sectionId++) {
        bfi_printf("Section ID %d\n", sectionId);
        rawSectionHeader = (BFIELFSectionHeader *) &this->OpenCLBinary[
                sectionStartOffset + sectionId * header->shentsize];

#if BFI_DEBUG
        printElfSectionHeader(rawSectionHeader);
#endif

        bfi_printf("Text: %s\n\n", (char *)&this->OpenCLBinary[
                this->innerELFOffset + sectionHeader->offset + rawSectionHeader->nameIdx]);
        
        // Clear the name field.
        SectionInformation.name.clear();
        sectionNameOffset = this->innerELFOffset + sectionHeader->offset + rawSectionHeader->nameIdx;
        // Read until a null, copying the bytes to the string.
        while (this->OpenCLBinary[sectionNameOffset]) {
            SectionInformation.name += (char) this->OpenCLBinary[sectionNameOffset];
            sectionNameOffset++;
        }
        SectionInformation.offset = rawSectionHeader->offset;
        SectionInformation.size = rawSectionHeader->size;
        this->ElfSections.push_back(SectionInformation);
    }
}