From c639d071e53d5565bb4df1157bcd133bbb4fb002 Mon Sep 17 00:00:00 2001 From: uyjulian Date: Mon, 14 Mar 2022 19:52:20 -0500 Subject: [PATCH] Add elftypechanger tool --- tools/Makefile | 8 +- tools/elftypechanger/Makefile | 14 +++ tools/elftypechanger/src/elftypechanger.c | 102 ++++++++++++++++++++++ 3 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 tools/elftypechanger/Makefile create mode 100644 tools/elftypechanger/src/elftypechanger.c diff --git a/tools/Makefile b/tools/Makefile index 8a2884247a50..8d0e6bf16f6d 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -6,7 +6,13 @@ # Licenced under Academic Free License version 2.0 # Review ps2sdk README & LICENSE files for further details. -SUBDIRS = bin2s bin2c bin2o adpenc ps2adpcm \ +SUBDIRS = \ + adpenc \ + bin2c \ + bin2o \ + bin2s \ + elftypechanger \ + ps2adpcm \ # gensymtab include $(PS2SDKSRC)/Defs.make diff --git a/tools/elftypechanger/Makefile b/tools/elftypechanger/Makefile new file mode 100644 index 000000000000..ed23b67aea30 --- /dev/null +++ b/tools/elftypechanger/Makefile @@ -0,0 +1,14 @@ +# _____ ___ ____ ___ ____ +# ____| | ____| | | |____| +# | ___| |____ ___| ____| | \ PS2DEV Open Source Project. +#----------------------------------------------------------------------- +# Copyright 2001-2004, ps2dev - http://www.ps2dev.org +# Licenced under Academic Free License version 2.0 +# Review ps2sdk README & LICENSE files for further details. + +TOOLS_OBJS = elftypechanger.o + +include $(PS2SDKSRC)/Defs.make +include $(PS2SDKSRC)/tools/Rules.bin.make +include $(PS2SDKSRC)/tools/Rules.make +include $(PS2SDKSRC)/tools/Rules.release diff --git a/tools/elftypechanger/src/elftypechanger.c b/tools/elftypechanger/src/elftypechanger.c new file mode 100644 index 000000000000..584b78764fb3 --- /dev/null +++ b/tools/elftypechanger/src/elftypechanger.c @@ -0,0 +1,102 @@ +/* +# _____ ___ ____ ___ ____ +# ____| | ____| | | |____| +# | ___| |____ ___| ____| | \ PS2DEV Open Source Project. +#----------------------------------------------------------------------- +# Copyright 2001-2022, ps2dev - http://www.ps2dev.org +# Licenced under Academic Free License version 2.0 +# Review ps2sdk README & LICENSE files for further details. +*/ + +#include +#include +#include +#include + +unsigned char *buffer; + +int main(int argc, char *argv[]) +{ + int fd_size; + FILE *source, *dest; + char *e_type_chr_end; + uint16_t e_type_int; + + if (argc != 4) + { + printf("elftypechanger\n" + "Usage: elftypechanger infile outfile e_type\n\n"); + return 1; + } + + if (argv[3][0] == '\x00') + { + printf("Argument 3 is empty.\n"); + return 1; + } + + e_type_chr_end = NULL; + e_type_int = (uint16_t)strtoul(argv[3], &e_type_chr_end, 16); + if (e_type_chr_end == NULL || *e_type_chr_end != '\x00') + { + printf("Argument 3 does is not a hexadecimal number.\n"); + return 1; + } + + if ((source = fopen(argv[1], "rb")) == NULL) + { + printf("Error opening %s for reading.\n",argv[1]); + return 1; + } + + fseek(source, 0, SEEK_END); + fd_size = ftell(source); + fseek(source, 0, SEEK_SET); + + buffer = malloc(fd_size); + if (buffer == NULL) + { + printf("Failed to allocate memory.\n"); + fclose(source); + return 1; + } + + if (fread(buffer, 1, fd_size, source) != fd_size) + { + printf("Failed to read file.\n"); + fclose(source); + return 1; + } + fclose(source); + + if (fd_size < 18) + { + printf("File is too small to be an ELF.\n"); + return 1; + } + + if (memcmp("\177ELF", buffer, 4) != 0) + { + printf("File does not appear to be ELF.\n"); + return 1; + } + + *((uint16_t *)(buffer + 16)) = e_type_int; + + if ((dest = fopen(argv[2], "w")) == NULL) + { + printf("Failed to open/create %s.\n", argv[2]); + return 1; + } + + if (fwrite(buffer, 1, fd_size, dest) != fd_size) + { + printf("Failed to write modified file.\n"); + fclose(dest); + return 1; + } + + fclose(dest); + + return 0; +}