completely moved "code"+"exhdr" parsing to elf.c

This commit is contained in:
applestash
2014-08-30 01:47:03 +10:00
parent 9828b8b061
commit 8eb1859158
3 changed files with 48 additions and 25 deletions
+36 -2
View File
@@ -1,5 +1,6 @@
#include "lib.h" #include "lib.h"
#include "ncch_build.h" #include "ncch_build.h"
#include "exheader_read.h"
#include "elf_hdr.h" #include "elf_hdr.h"
#include "elf.h" #include "elf.h"
#include "blz.h" #include "blz.h"
@@ -140,7 +141,10 @@ int ImportExeFsCodeBinaryFromFile(ncch_settings *set)
{ {
u32 size = set->componentFilePtrs.codeSize; u32 size = set->componentFilePtrs.codeSize;
u8 *buffer = malloc(size); 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); ReadFile64(buffer,size,0,set->componentFilePtrs.code);
set->exefsSections.code.size = set->componentFilePtrs.codeSize; set->exefsSections.code.size = set->componentFilePtrs.codeSize;
@@ -157,6 +161,36 @@ int ImportExeFsCodeBinaryFromFile(ncch_settings *set)
set->exefsSections.code.size = size; set->exefsSections.code.size = size;
set->exefsSections.code.buffer = buffer; 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; return 0;
} }
@@ -269,7 +303,7 @@ int CreateExeFsCode(elf_context *elf, u8 *elfFile, ncch_settings *set)
set->exefsSections.code.buffer = code; 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.textAddress = text.address;
set->codeDetails.textMaxPages = text.maxPageNum; set->codeDetails.textMaxPages = text.maxPageNum;
set->codeDetails.textSize = text.size; set->codeDetails.textSize = text.size;
+9 -20
View File
@@ -121,17 +121,6 @@ int get_ExHeaderSettingsFromNcchset(exheader_settings *exhdrset, ncch_settings *
fprintf(stderr,"[EXHEADER ERROR] Not enough memory\n"); fprintf(stderr,"[EXHEADER ERROR] Not enough memory\n");
return MEM_ERROR; 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 */ /* Create ExHeader Struct for output */
exhdrset->exHdr = (extended_hdr*)ncchset->sections.exhdr.buffer; exhdrset->exHdr = (extended_hdr*)ncchset->sections.exhdr.buffer;
@@ -142,17 +131,17 @@ int get_ExHeaderSettingsFromNcchset(exheader_settings *exhdrset, ncch_settings *
/* BSS Size */ /* BSS Size */
u32_to_u8(exhdrset->exHdr->codeSetInfo.bssSize,ncchset->codeDetails.bssSize,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.bssSize,ncchset->codeDetails.bssSize,LE);
/* Data */ /* Data */
u32_to_u8(exhdrset->exHdr->codeSetInfo.dataSectionInfo.address,ncchset->codeDetails.rwAddress,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.data.address,ncchset->codeDetails.rwAddress,LE);
u32_to_u8(exhdrset->exHdr->codeSetInfo.dataSectionInfo.codeSize,ncchset->codeDetails.rwSize,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.data.codeSize,ncchset->codeDetails.rwSize,LE);
u32_to_u8(exhdrset->exHdr->codeSetInfo.dataSectionInfo.numMaxPages,ncchset->codeDetails.rwMaxPages,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.data.numMaxPages,ncchset->codeDetails.rwMaxPages,LE);
/* RO */ /* RO */
u32_to_u8(exhdrset->exHdr->codeSetInfo.readOnlySectionInfo.address,ncchset->codeDetails.roAddress,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.rodata.address,ncchset->codeDetails.roAddress,LE);
u32_to_u8(exhdrset->exHdr->codeSetInfo.readOnlySectionInfo.codeSize,ncchset->codeDetails.roSize,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.rodata.codeSize,ncchset->codeDetails.roSize,LE);
u32_to_u8(exhdrset->exHdr->codeSetInfo.readOnlySectionInfo.numMaxPages,ncchset->codeDetails.roMaxPages,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.rodata.numMaxPages,ncchset->codeDetails.roMaxPages,LE);
/* Text */ /* Text */
u32_to_u8(exhdrset->exHdr->codeSetInfo.textSectionInfo.address,ncchset->codeDetails.textAddress,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.text.address,ncchset->codeDetails.textAddress,LE);
u32_to_u8(exhdrset->exHdr->codeSetInfo.textSectionInfo.codeSize,ncchset->codeDetails.textSize,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.text.codeSize,ncchset->codeDetails.textSize,LE);
u32_to_u8(exhdrset->exHdr->codeSetInfo.textSectionInfo.numMaxPages,ncchset->codeDetails.textMaxPages,LE); u32_to_u8(exhdrset->exHdr->codeSetInfo.text.numMaxPages,ncchset->codeDetails.textMaxPages,LE);
} }
/* Set Simple Flags */ /* Set Simple Flags */
+3 -3
View File
@@ -99,11 +99,11 @@ typedef struct
u8 padding0[5]; u8 padding0[5];
u8 flag; u8 flag;
u8 remasterVersion[2]; // le u16 u8 remasterVersion[2]; // le u16
exhdr_CodeSegmentInfo textSectionInfo; exhdr_CodeSegmentInfo text;
u8 stackSize[4]; // le u32 u8 stackSize[4]; // le u32
exhdr_CodeSegmentInfo readOnlySectionInfo; exhdr_CodeSegmentInfo rodata;
u8 padding1[4]; u8 padding1[4];
exhdr_CodeSegmentInfo dataSectionInfo; exhdr_CodeSegmentInfo data;
u8 bssSize[4]; // le u32 u8 bssSize[4]; // le u32
} exhdr_CodeSetInfo; } exhdr_CodeSetInfo;