Linux build fixes
[clinton/Virtual-Jaguar-Rx.git] / src / file.cpp
index 7e9fb34..50e9af0 100644 (file)
 // Who  When        What\r
 // ---  ----------  ------------------------------------------------------------\r
 // JLH  01/16/2010  Created this log ;-)\r
-// JLH  02/28/2010  Added functions to look inside .ZIP files and handle\r
-//                  contents\r
+// JLH  02/28/2010  Added functions to look inside .ZIP files and handle contents\r
 // JLH  06/01/2012  Added function to check ZIP file CRCs against file DB\r
-// JPM  06/06/2016  Visual Studio support\r
-// JPM  06/15/2016  ELF format support\r
-// JPM  06/19/2016  Soft debugger support\r
+// JPM   June/2016  Visual Studio support, ELF format support and Soft debugger support\r
 // JPM  07/15/2016  DWARF format support\r
+// JPM  04/06/2019  Added ELF sections check\r
+// JPM  03/12/2020  Added ELF section types check and new error messages\r
+// JPM   Aug./2020  ELF executable file information\r
 //\r
 \r
 #include "file.h"\r
@@ -26,6 +26,8 @@
 #endif // _MSC_VER\r
 #include <stdarg.h>\r
 #include <string.h>\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
 #include "crc32.h"\r
 #include "filedb.h"\r
 #include "eeprom.h"\r
 #include "universalhdr.h"\r
 #include "unzip.h"\r
 #include "zlib.h"\r
-#include "libelf/libelf.h"\r
-#include "libelf/gelf.h"\r
+#include "libelf.h"\r
+#include "gelf.h"\r
 #include "libdwarf.h"\r
-<<<<<<< HEAD\r
 #include "debugger/ELFManager.h"\r
 #include "debugger/DBGManager.h"\r
-=======\r
-#include "debugger\ELFManager.h"\r
-#include "debugger\DBGManager.h"\r
->>>>>>> origin/master\r
+#include "settings.h"\r
 \r
 \r
 // Private function prototypes\r
@@ -152,6 +150,7 @@ bool JaguarLoadFile(char * path)
        int     DBGType = DBG_NO_TYPE;\r
        bool error;\r
        int err;\r
+       struct stat _statbuf;\r
 \r
        jaguarROMSize = JaguarLoadROM(buffer, path);\r
 \r
@@ -205,9 +204,13 @@ WriteLog("FILE: Cartridge run address is reported as $%X...\n", jaguarRunAddress
 \r
                if (PtrELFExe != NULL)\r
                {\r
+                       // check the ELF version\r
                        if ((elf_version(EV_CURRENT) != EV_NONE) && (ElfMem = ELFManager_MemOpen(PtrELFExe, jaguarROMSize)))\r
                        {\r
-                               if (ELFManager_DwarfInit(ElfMem))\r
+                               // get the file information\r
+                               stat(path, &_statbuf);\r
+\r
+                               if (ELFManager_DwarfInit(ElfMem, _statbuf))\r
                                {\r
                                        DBGType |= DBG_ELFDWARF;\r
                                }\r
@@ -227,21 +230,21 @@ WriteLog("FILE: Cartridge run address is reported as $%X...\n", jaguarRunAddress
                                                        else\r
                                                        {\r
                                                                NameSection = elf_strptr(ElfMem, PtrGElfEhdr->e_shstrndx, (size_t)PtrGElfShdr->sh_name);\r
-                                                               WriteLog("FILE: ELF Section name: %s\n", NameSection);\r
+                                                               WriteLog("FILE: ELF Section %s found\n", NameSection);\r
 \r
-                                                               if ((ElfSectionNameType = ELFManager_GetSectionType(NameSection)) == ELF_NO_TYPE)\r
+                                                               if (((ElfSectionNameType = ELFManager_GetSectionType(NameSection)) == ELF_NO_TYPE) && vjs.ELFSectionsCheck)\r
                                                                {\r
-                                                                       WriteLog("FILE: ELF Section not listed\n");\r
+                                                                       WriteLog("FILE: ELF Section %s not recognized\n", NameSection);\r
                                                                        error = true;\r
                                                                }\r
                                                                else\r
                                                                {\r
                                                                        switch (PtrGElfShdr->sh_type)\r
                                                                        {\r
-                                                                       case    SHT_NULL:\r
+                                                                       case SHT_NULL:\r
                                                                                break;\r
 \r
-                                                                       case    SHT_PROGBITS:\r
+                                                                       case SHT_PROGBITS:\r
                                                                                if ((PtrGElfShdr->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR)))\r
                                                                                {\r
                                                                                        if (PtrGElfShdr->sh_addr >= 0x800000)\r
@@ -258,41 +261,52 @@ WriteLog("FILE: Cartridge run address is reported as $%X...\n", jaguarRunAddress
                                                                                {\r
                                                                                        switch (ElfSectionNameType)\r
                                                                                        {\r
+                                                                                       case ELF_debug_TYPE:\r
+                                                                                       case ELF_debug_abbrev_TYPE:\r
                                                                                        case ELF_debug_aranges_TYPE:\r
+                                                                                       case ELF_debug_frame_TYPE:\r
                                                                                        case ELF_debug_info_TYPE:\r
-                                                                                       case ELF_debug_abbrev_TYPE:\r
                                                                                        case ELF_debug_line_TYPE:\r
-                                                                                       case ELF_debug_frame_TYPE:\r
+                                                                                       case ELF_debug_loc_TYPE:\r
+                                                                                       case ELF_debug_macinfo_TYPE:\r
+                                                                                       case ELF_debug_pubnames_TYPE:\r
+                                                                                       case ELF_debug_pubtypes_TYPE:\r
                                                                                        case ELF_debug_ranges_TYPE:\r
                                                                                        case ELF_debug_str_TYPE:\r
-                                                                                       case ELF_debug_loc_TYPE:\r
+                                                                                       case ELF_debug_types_TYPE:                                              \r
+                                                                                               break;\r
+\r
+                                                                                       case ELF_heap_TYPE:\r
                                                                                                break;\r
 \r
                                                                                        case ELF_comment_TYPE:\r
                                                                                                break;\r
 \r
                                                                                        default:\r
+                                                                                               WriteLog("FILE: ELF section %s is not recognized\n", NameSection);\r
                                                                                                error = true;\r
                                                                                                break;\r
                                                                                        }\r
                                                                                }\r
                                                                                break;\r
 \r
-                                                                       case    SHT_NOBITS:\r
+                                                                       case SHT_NOBITS:\r
                                                                                break;\r
 \r
-                                                                       case    SHT_STRTAB:\r
-                                                                       case    SHT_SYMTAB:\r
+                                                                       case SHT_STRTAB:\r
+                                                                       case SHT_SYMTAB:\r
                                                                                while ((error == false) && ((PtrElfData = elf_getdata(PtrElfScn, PtrElfData)) != NULL))\r
                                                                                {\r
                                                                                        if (!ELFManager_AddTab(PtrElfData, ElfSectionNameType))\r
                                                                                        {\r
+                                                                                               WriteLog("FILE: ELF tab cannot be allocated\n");\r
                                                                                                error = true;\r
                                                                                        }\r
                                                                                }\r
                                                                                break;\r
 \r
                                                                        default:\r
+                                                                               WriteLog("FILE: ELF SHT type %i not recognized\n", PtrGElfShdr->sh_type);\r
                                                                                error = true;\r
                                                                                break;\r
                                                                        }\r
@@ -300,6 +314,7 @@ WriteLog("FILE: Cartridge run address is reported as $%X...\n", jaguarRunAddress
                                                        }\r
                                                }\r
 \r
+                                               // Set the executable address\r
                                                jaguarRunAddress = (uint32_t)PtrGElfEhdr->e_entry;\r
                                                WriteLog("FILE: Setting up ELF 32bits... Run address: %08X\n", jaguarRunAddress);\r
                                        }\r
@@ -310,6 +325,7 @@ WriteLog("FILE: Cartridge run address is reported as $%X...\n", jaguarRunAddress
                                }\r
                                else\r
                                {\r
+                                       WriteLog("FILE: Cannot get the number of the ELF sections\n");\r
                                        error = true;\r
                                }\r
                        }\r