diff --git a/makerom/elf.c b/makerom/elf.c index 03adee2..7bd07de 100644 --- a/makerom/elf.c +++ b/makerom/elf.c @@ -1,5 +1,6 @@ #include "lib.h" #include "ncch_build.h" +#include "exheader_read.h" #include "elf_hdr.h" #include "elf.h" #include "blz.h" @@ -140,7 +141,10 @@ int ImportExeFsCodeBinaryFromFile(ncch_settings *set) { u32 size = set->componentFilePtrs.codeSize; u8 *buffer = malloc(size); - if(!buffer) {fprintf(stderr,"[ELF ERROR] Not enough memory\n"); return MEM_ERROR;} + if(!buffer) { + fprintf(stderr,"[ELF ERROR] Not enough memory\n"); + return MEM_ERROR; + } ReadFile64(buffer,size,0,set->componentFilePtrs.code); set->exefsSections.code.size = set->componentFilePtrs.codeSize; @@ -157,6 +161,36 @@ int ImportExeFsCodeBinaryFromFile(ncch_settings *set) set->exefsSections.code.size = size; set->exefsSections.code.buffer = buffer; } + + size = set->componentFilePtrs.exhdrSize; + if(size < sizeof(extended_hdr)){ + fprintf(stderr,"[ELF ERROR] Exheader code info template is too small\n"); + return FAILED_TO_IMPORT_FILE; + } + extended_hdr *exhdr = malloc(size); + if(!exhdr) { + fprintf(stderr,"[ELF ERROR] Not enough memory\n"); + return MEM_ERROR; + } + ReadFile64(exhdr,size,0,set->componentFilePtrs.exhdr); + + /* Setting code_segment data */ + set->codeDetails.textAddress = u8_to_u32(exhdr->codeSetInfo.text.address,LE); + set->codeDetails.textMaxPages = u8_to_u32(exhdr->codeSetInfo.text.numMaxPages,LE); + set->codeDetails.textSize = u8_to_u32(exhdr->codeSetInfo.text.codeSize,LE); + + set->codeDetails.roAddress = u8_to_u32(exhdr->codeSetInfo.rodata.address,LE); + set->codeDetails.roMaxPages = u8_to_u32(exhdr->codeSetInfo.rodata.numMaxPages,LE); + set->codeDetails.roSize = u8_to_u32(exhdr->codeSetInfo.rodata.codeSize,LE); + + set->codeDetails.rwAddress = u8_to_u32(exhdr->codeSetInfo.data.address,LE); + set->codeDetails.rwMaxPages = u8_to_u32(exhdr->codeSetInfo.data.numMaxPages,LE); + set->codeDetails.rwSize = u8_to_u32(exhdr->codeSetInfo.data.codeSize,LE); + + set->codeDetails.bssSize = u8_to_u32(exhdr->codeSetInfo.bssSize,LE); + + free(exhdr); + return 0; } @@ -269,7 +303,7 @@ int CreateExeFsCode(elf_context *elf, u8 *elfFile, ncch_settings *set) set->exefsSections.code.buffer = code; } - /* Setting code_segment rwdata and freeing original buffers */ + /* Setting code_segment data and freeing original buffers */ set->codeDetails.textAddress = text.address; set->codeDetails.textMaxPages = text.maxPageNum; set->codeDetails.textSize = text.size; diff --git a/makerom/exheader.c b/makerom/exheader.c index 9c99f25..d7dba89 100644 --- a/makerom/exheader.c +++ b/makerom/exheader.c @@ -121,17 +121,6 @@ int get_ExHeaderSettingsFromNcchset(exheader_settings *exhdrset, ncch_settings * fprintf(stderr,"[EXHEADER ERROR] Not enough memory\n"); return MEM_ERROR; } - - /* Import ExHeader Code Section template */ - if(ncchset->componentFilePtrs.exhdrSize){ - u32 import_size = 0x30; //min64(0x30,ncchset->componentFilePtrs.exhdrSize); - u32 import_offset = 0x10; - if((import_size+import_offset) > ncchset->componentFilePtrs.exhdrSize){ - fprintf(stderr,"[EXHEADER ERROR] Exheader Template is too small\n"); - return FAILED_TO_IMPORT_FILE; - } - ReadFile64((ncchset->sections.exhdr.buffer+import_offset),import_size,import_offset,ncchset->componentFilePtrs.exhdr); - } /* Create ExHeader Struct for output */ exhdrset->exHdr = (extended_hdr*)ncchset->sections.exhdr.buffer; @@ -142,17 +131,17 @@ int get_ExHeaderSettingsFromNcchset(exheader_settings *exhdrset, ncch_settings * /* BSS Size */ u32_to_u8(exhdrset->exHdr->codeSetInfo.bssSize,ncchset->codeDetails.bssSize,LE); /* Data */ - u32_to_u8(exhdrset->exHdr->codeSetInfo.dataSectionInfo.address,ncchset->codeDetails.rwAddress,LE); - u32_to_u8(exhdrset->exHdr->codeSetInfo.dataSectionInfo.codeSize,ncchset->codeDetails.rwSize,LE); - u32_to_u8(exhdrset->exHdr->codeSetInfo.dataSectionInfo.numMaxPages,ncchset->codeDetails.rwMaxPages,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.data.address,ncchset->codeDetails.rwAddress,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.data.codeSize,ncchset->codeDetails.rwSize,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.data.numMaxPages,ncchset->codeDetails.rwMaxPages,LE); /* RO */ - u32_to_u8(exhdrset->exHdr->codeSetInfo.readOnlySectionInfo.address,ncchset->codeDetails.roAddress,LE); - u32_to_u8(exhdrset->exHdr->codeSetInfo.readOnlySectionInfo.codeSize,ncchset->codeDetails.roSize,LE); - u32_to_u8(exhdrset->exHdr->codeSetInfo.readOnlySectionInfo.numMaxPages,ncchset->codeDetails.roMaxPages,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.rodata.address,ncchset->codeDetails.roAddress,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.rodata.codeSize,ncchset->codeDetails.roSize,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.rodata.numMaxPages,ncchset->codeDetails.roMaxPages,LE); /* Text */ - u32_to_u8(exhdrset->exHdr->codeSetInfo.textSectionInfo.address,ncchset->codeDetails.textAddress,LE); - u32_to_u8(exhdrset->exHdr->codeSetInfo.textSectionInfo.codeSize,ncchset->codeDetails.textSize,LE); - u32_to_u8(exhdrset->exHdr->codeSetInfo.textSectionInfo.numMaxPages,ncchset->codeDetails.textMaxPages,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.text.address,ncchset->codeDetails.textAddress,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.text.codeSize,ncchset->codeDetails.textSize,LE); + u32_to_u8(exhdrset->exHdr->codeSetInfo.text.numMaxPages,ncchset->codeDetails.textMaxPages,LE); } /* Set Simple Flags */ diff --git a/makerom/exheader.h b/makerom/exheader.h index d2d216f..ee1e2d4 100644 --- a/makerom/exheader.h +++ b/makerom/exheader.h @@ -99,11 +99,11 @@ typedef struct u8 padding0[5]; u8 flag; u8 remasterVersion[2]; // le u16 - exhdr_CodeSegmentInfo textSectionInfo; + exhdr_CodeSegmentInfo text; u8 stackSize[4]; // le u32 - exhdr_CodeSegmentInfo readOnlySectionInfo; + exhdr_CodeSegmentInfo rodata; u8 padding1[4]; - exhdr_CodeSegmentInfo dataSectionInfo; + exhdr_CodeSegmentInfo data; u8 bssSize[4]; // le u32 } exhdr_CodeSetInfo;