From f786346d3c0dcf2e0d11d36d89221d90871821c0 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 | 2 +- tools/elftypechanger/Makefile | 26 ++++++ tools/elftypechanger/src/elftypechanger.c | 102 ++++++++++++++++++++++ 3 files changed, 129 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..f33611821ec5 100644 --- a/tools/Makefile +++ b/tools/Makefile @@ -6,7 +6,7 @@ # Licenced under Academic Free License version 2.0 # Review ps2sdk README & LICENSE files for further details. -SUBDIRS = bin2s bin2c bin2o adpenc ps2adpcm \ +SUBDIRS = bin2s bin2c bin2o adpenc ps2adpcm elftypechanger \ # gensymtab include $(PS2SDKSRC)/Defs.make diff --git a/tools/elftypechanger/Makefile b/tools/elftypechanger/Makefile new file mode 100644 index 000000000000..7e858828bc02 --- /dev/null +++ b/tools/elftypechanger/Makefile @@ -0,0 +1,26 @@ +# _____ ___ ____ ___ ____ +# ____| | ____| | | |____| +# | ___| |____ ___| ____| | \ 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. + +TOOLS_OBJS_DIR = obj/ +TOOLS_BIN_DIR = bin/ +TOOLS_SRC_DIR = src/ +TOOLS_INC_DIR = include/ + +TOOLS_OBJS = elftypechanger.o +TOOLS_OBJS := $(TOOLS_OBJS:%=$(TOOLS_OBJS_DIR)%) + +TOOLS_BIN = $(TOOLS_BIN_DIR)elftypechanger + +all: $(TOOLS_OBJS_DIR) $(TOOLS_BIN_DIR) $(TOOLS_BIN) + +clean: + rm -f -r $(TOOLS_OBJS_DIR) $(TOOLS_BIN_DIR) + +include $(PS2SDKSRC)/Defs.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; +}