// convert an elf file to a dol file // // args: dol file object // elf filename // returns: true if successful bool convertElfFileToDol(DolFile& dolFile, const char *elfFilename) { // open elf input file ElfFile elfFile; if(!elfFile.open(elfFilename)) return false; if(!elfFile.isPpc()) printf("Elf file doesnt seem to be for PPC!\n"); // create a temp file to store section data during copy FILE* temp_fd = fopen("~temp.tmp", "w+b"); if(temp_fd == 0) { elfFile.close(); return false; } // check which sections to add to dol file for(u32 i=0; i<elfFile.getNumSections(); i++) { // check if section type is 'PROGBITS' // these are the sections required for the dol file if(elfFile.getSectionType(i) == SHT_PROGBITS) { // get section data from elf file fseek(temp_fd, 0, SEEK_SET); elfFile.extractSectionData(i, temp_fd); fseek(temp_fd, 0, SEEK_SET); // sections with 'EXEC' flags are program code, and so // should be added as a TEXT section in the dol file if(elfFile.getSectionFlags(i) & SHF_EXECINSTR) { // add section data to dol file dolFile.addTextSection(temp_fd, elfFile.getSectionAddress(i), elfFile.getSectionSize(i)); } else { // add section data to dol file if (elfFile.getSectionAddress(i) != 0 && elfFile.getSectionSize(i) != 0) dolFile.addDataSection(temp_fd, elfFile.getSectionAddress(i), elfFile.getSectionSize(i)); } } else { // check for bss section by checking section name for '.bss' char *name = 0; if(elfFile.getSectionName(i, name) != ERROR_RETURN) { if(strcasecmp(name, ".bss") == 0) { dolFile.setBssAddress(elfFile.getSectionAddress(i)); dolFile.setBssSize(elfFile.getSectionSize(i)); } delete[] name; } } } // set dol files entry point dolFile.setEntryPoint(elfFile.getEntryPoint()); // finish up fclose(temp_fd); remove("~temp.tmp"); elfFile.close(); printf( "\nConverted %s to %s\n", elfFilename, dolFile.getFilename()); return true; }
// dump dol file to an elf file // // args: dol file object // returns: true if successful bool dumpDolToElfFile(const DolFile& dolFile) { // work out filename to use for elf file char elfFilename[256]; strcpy(elfFilename, dolFile.getFilename()); ChangeFileExtension(elfFilename, "elf"); // create elf output file ElfFile elfFile; if(!elfFile.create(elfFilename, ELFDATA2MSB)) return false; // set elf file as PPC elfFile.setMachineType(EM_PPC); // set entry point in elf file elfFile.setEntryPoint(dolFile.getEntryPoint()); // create a temp file to store section data during copy FILE* temp_fd = fopen("~temp.tmp", "w+b"); if(temp_fd == 0) { elfFile.close(); return false; } char name[32]; // add dol text sections to elf file for(u32 i=0; i<dolFile.getNumTextSections(); i++) { // get section data from dol file fseek(temp_fd, 0, SEEK_SET); dolFile.extractTextSection(i, temp_fd); sprintf(name, ".text%d", i); // add section data to elf file fseek(temp_fd, 0, SEEK_SET); elfFile.addSection(temp_fd, dolFile.getTextAddress(i), dolFile.getTextSize(i), name, SHT_PROGBITS, SHF_ALLOC|SHF_EXECINSTR, 32); // add a program header for the section just added // the section just added, was added as the last section // u32 offset = elfFile.getSectionOffset(elfFile.getNumSections() - 1); // elfFile.addProgramHeader(dolFile.getTextAddress(i), dolFile.getTextSize(i), offset, PT_LOAD, PF_EXEC|PF_READ, 32); } // add dol data sections to elf file for(u32 i=0; i<dolFile.getNumDataSections(); i++) { // get section data from dol file fseek(temp_fd, 0, SEEK_SET); dolFile.extractDataSection(i, temp_fd); sprintf(name, ".data%d", i); // add section data to elf file fseek(temp_fd, 0, SEEK_SET); elfFile.addSection(temp_fd, dolFile.getDataAddress(i), dolFile.getDataSize(i), name, SHT_PROGBITS, SHF_ALLOC|SHF_WRITE, 32); // add a program header for the section just added // the section just added, was added as the last section // u32 offset = elfFile.getSectionOffset(elfFile.getNumSections() - 1); // elfFile.addProgramHeader(dolFile.getDataAddress(i), dolFile.getDataSize(i), offset, PT_LOAD, PF_WRITE|PF_READ, 32); } // add bss section to elf file elfFile.addSectionHeader(dolFile.getBssAddress(), dolFile.getBssSize(), ".bss", 0, SHT_NOBITS, SHF_WRITE|SHF_ALLOC, 1); // add a program header for the section just added // the section just added, was added as the last section // u32 index = elfFile.getNumSections() - 1; // elfFile.addProgramHeader(elfFile.getSectionAddress(index), elfFile.getSectionSize(index), elfFile.getSectionOffset(index), PT_LOAD, PF_WRITE|PF_READ, 32); // finish up fclose(temp_fd); remove("~temp.tmp"); elfFile.close(); printf( "\nConverted %s to to %s\n", dolFile.getFilename(), elfFilename); return true; }