makerom v0.2

This commit is contained in:
3DSGuy
2014-02-21 03:14:00 +08:00
parent 0155b2098e
commit 3c9fffd56a
24 changed files with 1825 additions and 1097 deletions
+111 -111
View File
@@ -157,8 +157,8 @@ int ImportExeFsCodeBinaryFromFile(ncch_settings *ncchset)
u32 GetPageSize(ncch_settings *ncchset)
{
if(ncchset->yaml_set->DefaultSpec.Option.PageSize)
return strtoul(ncchset->yaml_set->DefaultSpec.Option.PageSize,NULL,10);
if(ncchset->yaml_set->Option.PageSize)
return strtoul(ncchset->yaml_set->Option.PageSize,NULL,10);
return 0x1000;
}
@@ -181,16 +181,16 @@ int GetBSS_SizeFromElf(ElfContext *elf, u8 *ElfFile, ncch_settings *ncchset)
int ImportPlainRegionFromElf(ElfContext *elf, u8 *ElfFile, ncch_settings *ncchset) // Doesn't work same as N makerom
{
if(!ncchset->yaml_set->DefaultSpec.PlainRegionNum) return 0;
u16 *Index = malloc(sizeof(u16)*ncchset->yaml_set->DefaultSpec.PlainRegionNum);
if(!ncchset->yaml_set->PlainRegionNum) return 0;
u16 *Index = malloc(sizeof(u16)*ncchset->yaml_set->PlainRegionNum);
/* Getting Index Values for each section */
for(int i = 0; i < ncchset->yaml_set->DefaultSpec.PlainRegionNum; i++){
Index[i] = GetElfSectionIndexFromName(ncchset->yaml_set->DefaultSpec.PlainRegion[i],elf,ElfFile);
for(int i = 0; i < ncchset->yaml_set->PlainRegionNum; i++){
Index[i] = GetElfSectionIndexFromName(ncchset->yaml_set->PlainRegion[i],elf,ElfFile);
}
// Eliminating Duplicated Sections
for(int i = ncchset->yaml_set->DefaultSpec.PlainRegionNum - 1; i >= 0; i--){
for(int i = ncchset->yaml_set->PlainRegionNum - 1; i >= 0; i--){
for(int j = i-1; j >= 0; j--){
if(Index[i] == Index[j]) Index[i] = 0;
}
@@ -198,7 +198,7 @@ int ImportPlainRegionFromElf(ElfContext *elf, u8 *ElfFile, ncch_settings *ncchse
/* Calculating Total Size of Data */
u64 TotalSize = 0;
for(int i = 0; i < ncchset->yaml_set->DefaultSpec.PlainRegionNum; i++){
for(int i = 0; i < ncchset->yaml_set->PlainRegionNum; i++){
TotalSize += elf->Sections[Index[i]].Size;
}
@@ -210,7 +210,7 @@ int ImportPlainRegionFromElf(ElfContext *elf, u8 *ElfFile, ncch_settings *ncchse
/* Storing Sections */
u64 pos = 0;
for(int i = 0; i < ncchset->yaml_set->DefaultSpec.PlainRegionNum; i++){
for(int i = 0; i < ncchset->yaml_set->PlainRegionNum; i++){
memcpy((ncchset->Sections.PlainRegion.buffer+pos),elf->Sections[Index[i]].Ptr,elf->Sections[Index[i]].Size);
pos += elf->Sections[Index[i]].Size;
}
@@ -227,11 +227,11 @@ int CreateExeFsCode(ElfContext *elf, u8 *ElfFile, ncch_settings *ncchset)
CodeSegment Data;
memset(&Data,0,sizeof(CodeSegment));
int result = CreateCodeSegmentFromElf(&Text,elf,ElfFile,ncchset->yaml_set->DefaultSpec.ExeFs.Text,ncchset->yaml_set->DefaultSpec.ExeFs.TextNum);
int result = CreateCodeSegmentFromElf(&Text,elf,ElfFile,ncchset->yaml_set->ExeFs.Text,ncchset->yaml_set->ExeFs.TextNum);
if(result) return result;
result = CreateCodeSegmentFromElf(&RO,elf,ElfFile,ncchset->yaml_set->DefaultSpec.ExeFs.ReadOnly,ncchset->yaml_set->DefaultSpec.ExeFs.ReadOnlyNum);
result = CreateCodeSegmentFromElf(&RO,elf,ElfFile,ncchset->yaml_set->ExeFs.ReadOnly,ncchset->yaml_set->ExeFs.ReadOnlyNum);
if(result) return result;
result = CreateCodeSegmentFromElf(&Data,elf,ElfFile,ncchset->yaml_set->DefaultSpec.ExeFs.ReadWrite,ncchset->yaml_set->DefaultSpec.ExeFs.ReadWriteNum);
result = CreateCodeSegmentFromElf(&Data,elf,ElfFile,ncchset->yaml_set->ExeFs.ReadWrite,ncchset->yaml_set->ExeFs.ReadWriteNum);
if(result) return result;
/* Allocating Buffer for ExeFs Code */
@@ -283,40 +283,40 @@ int CreateCodeSegmentFromElf(CodeSegment *out, ElfContext *elf, u8 *ElfFile, cha
{
u16 ContinuousSegmentNum = 0;
memset(out,0,sizeof(CodeSegment));
ElfSegment **ContinuousSegments = GetContinuousSegments(&ContinuousSegmentNum,elf,Names,NameNum);
if (ContinuousSegments == NULL){
if(!ContinuousSegmentNum) // Nothing Was Found
return 0;
else // Error with found segments
return ELF_SEGMENTS_NOT_CONTINUOUS;
}
/* Getting Segment Size/Settings */
u32 vAddr = 0;
u32 memorySize = 0;
for(int i = 0; i < ContinuousSegmentNum; i++){
if (i==0){
vAddr = ContinuousSegments[i]->VAddr;
}
else{ // Add rounded size from previous segment
u32 num = ContinuousSegments[i]->VAddr - (vAddr + memorySize);
memorySize += num;
}
memorySize += ContinuousSegments[i]->Header->SizeInMemory;
for (int j = 0; j < ContinuousSegments[i]->SectionNum; j++){
ElfSectionEntry *Section = &ContinuousSegments[i]->Sections[j];
if (IsBss(Section) && j == (ContinuousSegments[i]->SectionNum-1))
memorySize -= Section->Size;
}
}
// For Check
#ifdef ELF_DEBUG
printf("Address: 0x%x\n",vAddr);
printf("Size: 0x%x\n",memorySize);
#endif
ElfSegment **ContinuousSegments = GetContinuousSegments(&ContinuousSegmentNum,elf,Names,NameNum);
if (ContinuousSegments == NULL){
if(!ContinuousSegmentNum) // Nothing Was Found
return 0;
else // Error with found segments
return ELF_SEGMENTS_NOT_CONTINUOUS;
}
/* Getting Segment Size/Settings */
u32 vAddr = 0;
u32 memorySize = 0;
for(int i = 0; i < ContinuousSegmentNum; i++){
if (i==0){
vAddr = ContinuousSegments[i]->VAddr;
}
else{ // Add rounded size from previous segment
u32 num = ContinuousSegments[i]->VAddr - (vAddr + memorySize);
memorySize += num;
}
memorySize += ContinuousSegments[i]->Header->SizeInMemory;
for (int j = 0; j < ContinuousSegments[i]->SectionNum; j++){
ElfSectionEntry *Section = &ContinuousSegments[i]->Sections[j];
if (IsBss(Section) && j == (ContinuousSegments[i]->SectionNum-1))
memorySize -= Section->Size;
}
}
// For Check
#ifdef ELF_DEBUG
printf("Address: 0x%x\n",vAddr);
printf("Size: 0x%x\n",memorySize);
#endif
out->Address = vAddr;
out->Size = memorySize;
out->MaxPageNum = SizeToPage(memorySize,elf);
@@ -325,28 +325,28 @@ int CreateCodeSegmentFromElf(CodeSegment *out, ElfContext *elf, u8 *ElfFile, cha
/* Writing Segment to Buffer */
vAddr = 0;
memorySize = 0;
for(int i = 0; i < ContinuousSegmentNum; i++){
if (i==0){
vAddr = ContinuousSegments[i]->VAddr;
}
else{
u32 num = ContinuousSegments[i]->VAddr - (vAddr + memorySize);
memorySize += num;
}
u32 size = 0;
for (int j = 0; j < ContinuousSegments[i]->SectionNum; j++){
ElfSectionEntry *Section = &ContinuousSegments[i]->Sections[j];
if (!IsBss(Section)){
u8 *pos = (out->Data + memorySize + size);
memcpy(pos,Section->Ptr,Section->Size);
size += Section->Size;
}
else if (j == (ContinuousSegments[i]->SectionNum-1))
memorySize -= Section->Size;
else
size += Section->Size;
}
for(int i = 0; i < ContinuousSegmentNum; i++){
if (i==0){
vAddr = ContinuousSegments[i]->VAddr;
}
else{
u32 num = ContinuousSegments[i]->VAddr - (vAddr + memorySize);
memorySize += num;
}
u32 size = 0;
for (int j = 0; j < ContinuousSegments[i]->SectionNum; j++){
ElfSectionEntry *Section = &ContinuousSegments[i]->Sections[j];
if (!IsBss(Section)){
u8 *pos = (out->Data + memorySize + size);
memcpy(pos,Section->Ptr,Section->Size);
size += Section->Size;
}
else if (j == (ContinuousSegments[i]->SectionNum-1))
memorySize -= Section->Size;
else
size += Section->Size;
}
}
free(ContinuousSegments);
@@ -354,52 +354,52 @@ int CreateCodeSegmentFromElf(CodeSegment *out, ElfContext *elf, u8 *ElfFile, cha
}
ElfSegment** GetContinuousSegments(u16 *ContinuousSegmentNum, ElfContext *elf, char **Names, u32 NameNum)
{
u16 SegmentNum = 0;
ElfSegment **Segments = GetSegments(&SegmentNum, elf, Names, NameNum);
if (Segments == NULL || SegmentNum == 0){ // No Segments for the names were found
//printf("Not Found Segment\n");
return NULL;
}
if (SegmentNum == 1){ //Return as there is no need to check
*ContinuousSegmentNum = SegmentNum;
return Segments;
}
u32 vAddr = Segments[0]->VAddr + Segments[0]->Header->SizeInMemory;
for (int i = 1; i < SegmentNum; i++){
if (Segments[i]->VAddr != (u32)align_value(vAddr,Segments[i]->Header->Alignment)){ //Each Segment must start after each other
fprintf(stderr,"[ELF ERROR] %s segment and %s segment are not continuous\n", Segments[i]->Name, Segments[i - 1]->Name);
free(Segments);
*ContinuousSegmentNum = 0xffff; // Signify to function that an error occured
return NULL;
}
}
*ContinuousSegmentNum = SegmentNum;
return Segments;
ElfSegment** GetContinuousSegments(u16 *ContinuousSegmentNum, ElfContext *elf, char **Names, u32 NameNum)
{
u16 SegmentNum = 0;
ElfSegment **Segments = GetSegments(&SegmentNum, elf, Names, NameNum);
if (Segments == NULL || SegmentNum == 0){ // No Segments for the names were found
//printf("Not Found Segment\n");
return NULL;
}
if (SegmentNum == 1){ //Return as there is no need to check
*ContinuousSegmentNum = SegmentNum;
return Segments;
}
u32 vAddr = Segments[0]->VAddr + Segments[0]->Header->SizeInMemory;
for (int i = 1; i < SegmentNum; i++){
if (Segments[i]->VAddr != (u32)align_value(vAddr,Segments[i]->Header->Alignment)){ //Each Segment must start after each other
fprintf(stderr,"[ELF ERROR] %s segment and %s segment are not continuous\n", Segments[i]->Name, Segments[i - 1]->Name);
free(Segments);
*ContinuousSegmentNum = 0xffff; // Signify to function that an error occured
return NULL;
}
}
*ContinuousSegmentNum = SegmentNum;
return Segments;
}
ElfSegment** GetSegments(u16 *SegmentNum, ElfContext *elf, char **Names, u32 NameNum)
{
if (Names == NULL)
{
return NULL;
}
ElfSegment **Segments = malloc(sizeof(ElfSegment*)*NameNum);
*SegmentNum = 0; // There can be a max of NameNum Segments, however, they might not all exist
for (int i = 0; i < NameNum; i++){
for(int j = 0; j < elf->ActiveSegments; j++){
if(strcmp(Names[i],elf->Segments[j].Name) == 0){ // If there is a match, store Segment data pointer & increment index
Segments[*SegmentNum] = &elf->Segments[j];
*SegmentNum = *SegmentNum + 1;
}
}
}
return Segments;
ElfSegment** GetSegments(u16 *SegmentNum, ElfContext *elf, char **Names, u32 NameNum)
{
if (Names == NULL)
{
return NULL;
}
ElfSegment **Segments = malloc(sizeof(ElfSegment*)*NameNum);
*SegmentNum = 0; // There can be a max of NameNum Segments, however, they might not all exist
for (int i = 0; i < NameNum; i++){
for(int j = 0; j < elf->ActiveSegments; j++){
if(strcmp(Names[i],elf->Segments[j].Name) == 0){ // If there is a match, store Segment data pointer & increment index
Segments[*SegmentNum] = &elf->Segments[j];
*SegmentNum = *SegmentNum + 1;
}
}
}
return Segments;
}
// ELF Functions