Skip to content

Commit

Permalink
Merge pull request #7 from djipi/5-potential-crash-with-a-vma-lma-dif…
Browse files Browse the repository at this point in the history
…ferent-address-in-the-binary-elf

5 potential crash with a vma lma different address in the binary elf
  • Loading branch information
djipi authored Aug 20, 2024
2 parents 5f62210 + dba3aea commit 32a331c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
3 changes: 3 additions & 0 deletions jiffi2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Supported formats of the Atari Jaguar.
- ROM


Version 1.0.3 - 08-20-2024
- Fix potential crash with a VMA & LMA different address in the binary ELF

Version 1.0.2 - 06-23-2024
- Added a Visual Studio 2022 project
- Removed hardcoded libraries version in the about's UI
Expand Down
35 changes: 24 additions & 11 deletions src/format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,14 @@ int detect_file_format(void)
Elf_Scn* PtrElfScn;
if (((PtrGElfEhdr = gelf_getehdr(ElfMem, &ElfEhdr)) != NULL) && ((PtrElfScn = elf_getscn(ElfMem, 0)) != NULL))
{
// init offsets low/high
size_t offsetlow, offsethigh = 0;
// get load address
loadadr = -1;
// loop on the ELF information to get useful parts and loading address
GElf_Shdr GElfShdr, * PtrGElfShdr;
for (;(PtrElfScn != NULL); PtrElfScn = elf_nextscn(ElfMem, PtrElfScn))
GElf_Phdr GElfPhdr, * PtrGElfPhdr;
for (int index = 0; (PtrElfScn != NULL); PtrElfScn = elf_nextscn(ElfMem, PtrElfScn), index = 0)
{
PtrGElfShdr = gelf_getshdr(PtrElfScn, &GElfShdr);
switch (PtrGElfShdr->sh_type)
Expand All @@ -240,19 +243,27 @@ int detect_file_format(void)
case SHT_PROGBITS:
if ((PtrGElfShdr->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR)))
{
if (PtrGElfShdr->sh_addr < loadadr)
while ((PtrGElfPhdr = gelf_getphdr(ElfMem, index++, &GElfPhdr)) ? (PtrGElfPhdr->p_offset != PtrGElfShdr->sh_offset) : false);
if (PtrGElfPhdr)
{
loadadr = (unsigned int)(PtrGElfShdr->sh_addr);
if (ptr < (ptrload + PtrGElfShdr->sh_offset))
if (PtrGElfPhdr->p_paddr < loadadr)
{
ptr = (ptrload + PtrGElfShdr->sh_offset);
loadadr = (unsigned int)(PtrGElfPhdr->p_paddr);
if (ptr < (ptrload + PtrGElfPhdr->p_offset))
{
ptr = (ptrload + (offsetlow = PtrGElfPhdr->p_offset));
}
}
if (PtrGElfPhdr->p_offset >= offsethigh)
{
offsethigh = PtrGElfPhdr->p_offset + PtrGElfShdr->sh_size;
}
}
}
else
{
linj -= (unsigned int)(PtrGElfShdr->sh_size);
}
//else
//{
// linj -= (unsigned int)(PtrGElfShdr->sh_size);
//}
break;
// Symbol table
case SHT_SYMTAB:
Expand All @@ -264,14 +275,16 @@ int detect_file_format(void)
case SHT_NOBITS:
// reduce the size with the section's size
default:
linj -= (unsigned int)(PtrGElfShdr->sh_size);
//linj -= (unsigned int)(PtrGElfShdr->sh_size);
break;
}
}
// get run address
//runadr = !endianess ? (uint32_t)PtrGElfEhdr->e_entry : ((((uint32_t)PtrGElfEhdr->e_entry & 0xff000000) >> 24) | (((uint32_t)PtrGElfEhdr->e_entry & 0x00ff0000) >> 8) | (((uint32_t)PtrGElfEhdr->e_entry & 0x0000ff00) << 8) | (((uint32_t)PtrGElfEhdr->e_entry & 0x000000ff) << 24));
runadr = (uint32_t)PtrGElfEhdr->e_entry;
memcpy(imageadr, ptr, linj);
// copy the used code + data
memcpy(imageadr, ptr, (linj = offsethigh-offsetlow));
// ELF format considered correct
detected_format = format_ELF;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
// Release Information
#define MAJOR 1 // Major version number
#define MINOR 0 // Minor version number
#define PATCH 2 // Patch release number
#define PATCH 3 // Patch release number

#endif // __VERSION_H__
2 changes: 1 addition & 1 deletion vs2022/JiFFI2.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
<ObjectFileName>$(IntDir)obj\</ObjectFileName>
<CompileAsManaged>false</CompileAsManaged>
<OmitDefaultLibName>false</OmitDefaultLibName>
<WarningLevel>Level3</WarningLevel>
<WarningLevel>Level4</WarningLevel>
</ClCompile>
<Link>
<AdditionalLibraryDirectories>$(Qt_LIBPATH_);%(AdditionalLibraryDirectories);C:\SDK\ELF\libelf-0.8.13\lib;C:\SDK\crc\crc32\lib;C:\SDK\XML\tinyxml2-10.0.0\lib</AdditionalLibraryDirectories>
Expand Down

0 comments on commit 32a331c

Please sign in to comment.