mirror of
https://github.com/zeldaret/oot.git
synced 2025-08-13 10:21:18 +00:00
New relocation-generating program (#1016)
* Update makefiles * git subrepo clone git@github.com:EllipticEllipsis/fado.git tools/fado subrepo: subdir: "tools/fado" merged: "46c4d751a" upstream: origin: "git@github.com:EllipticEllipsis/fado.git" branch: "master" commit: "46c4d751a" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "2f68596" * git subrepo pull tools/fado subrepo: subdir: "tools/fado" merged: "88114ebce" upstream: origin: "git@github.com:EllipticEllipsis/fado.git" branch: "master" commit: "88114ebce" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "2f68596" * A few ideas for computing dependencies * Remove reserved identifiers from spec.h and util.h and add required headers * Fix a couple more headers * Program for reloc prerequisites * git subrepo pull tools/fado subrepo: subdir: "tools/fado" merged: "36a905f72" upstream: origin: "git@github.com:EllipticEllipsis/fado.git" branch: "master" commit: "36a905f72" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "2f68596" * Update makefile to make dependency files and use overlay's name * git subrepo pull tools/fado subrepo: subdir: "tools/fado" merged: "43c339a59" upstream: origin: "git@github.com:EllipticEllipsis/fado.git" branch: "master" commit: "43c339a59" git-subrepo: version: "0.4.3" origin: "https://github.com/ingydotnet/git-subrepo.git" commit: "2f68596" * Change awk to grep, delete ZAPD files, gitignore elf * Delete all the cfg files * Fix memory leaks * Rename and add coloured errors * Makefile tweaks - preprocess spec before grep - split order prerequisites via phony target to reduce dependency edges - remove `resources` target - remove separate overlays targets - use `$(SPEC)` throughout - change to using filenames of relocs for overlay names via `$*` - Rearrange targets to better reflect their categories * Update gitignore * Review * Add a check for the reloc file name * get_segment_by_name * get_stmt_id_by_stmt_name * Cleaning up * algorithm change * function rename * Fix typos Co-authored-by: angie <angheloalf95@gmail.com>
This commit is contained in:
parent
cf048f849a
commit
eadc477187
511 changed files with 3947 additions and 1104 deletions
229
tools/fado/src/main.c
Normal file
229
tools/fado/src/main.c
Normal file
|
@ -0,0 +1,229 @@
|
|||
/* Copyright (C) 2021 Elliptic Ellipsis */
|
||||
/* SPDX-License-Identifier: AGPL-3.0-only */
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include "macros.h"
|
||||
#include "fairy/fairy.h"
|
||||
#include "fado.h"
|
||||
#include "help.h"
|
||||
#include "mido.h"
|
||||
#include "vc_vector/vc_vector.h"
|
||||
|
||||
#include "version.inc"
|
||||
|
||||
void PrintVersion() {
|
||||
printf("Fado (Fairy-Assisted relocations for Decompiled Overlays), version %s\n", versionNumber);
|
||||
printf("Copyright (C) 2021 Elliptic Ellipsis\n");
|
||||
printf("%s\n", credits);
|
||||
printf("Repository available at %s.\n", repo);
|
||||
}
|
||||
|
||||
#if defined _WIN32 || defined __CYGWIN__
|
||||
#define PATH_SEPARATOR '\\'
|
||||
#else
|
||||
#define PATH_SEPARATOR '/'
|
||||
#endif
|
||||
|
||||
/**
|
||||
* (Bad) filename-parsing idea to get the overlay name from the filename. Output must be freed separately.
|
||||
*/
|
||||
char* GetOverlayNameFromFilename(const char* src) {
|
||||
char* ret;
|
||||
const char* ptr;
|
||||
const char* start = src;
|
||||
const char* end = src;
|
||||
|
||||
for (ptr = src; *ptr != '\0'; ptr++) {
|
||||
if (*ptr == PATH_SEPARATOR) {
|
||||
start = end + 1;
|
||||
end = ptr;
|
||||
}
|
||||
}
|
||||
|
||||
if (end == src) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ret = malloc((end - start + 1) * sizeof(char));
|
||||
memcpy(ret, start, end - start);
|
||||
ret[end - start] = '\0';
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#define OPTSTR "M:n:o:v:hV"
|
||||
#define USAGE_STRING "Usage: %s [-hV] [-n name] [-o output_file] [-v level] input_files ...\n"
|
||||
|
||||
#define HELP_PROLOGUE \
|
||||
"Fado (Fairy-Assisted relocations for Decompiled Overlays\n" \
|
||||
"Extract relocations from object files and convert them into the format required by Zelda 64 overlays.\n"
|
||||
#define HELP_EPILOGUE repo
|
||||
|
||||
// clang-format off
|
||||
static const PosArgInfo posArgInfo[] = {
|
||||
{ "INPUT_FILE", "Every positional argument is an input file, and there should be at least one input file. All inputs are relocatable .o (object) ELF files" },
|
||||
{ NULL, NULL }
|
||||
};
|
||||
|
||||
static const OptInfo optInfo[] = {
|
||||
{ { "make-dependency", required_argument, NULL, 'M' }, "FILE", "Write the output file's Makefile dependencies to FILE" },
|
||||
{ { "name", required_argument, NULL, 'n' }, "NAME", "Use NAME as the overlay name. Will use the deepest folder name in the input file's path if not specified" },
|
||||
{ { "output-file", required_argument, NULL, 'o' }, "FILE", "Output to FILE. Will use stdout if none is specified" },
|
||||
{ { "verbosity", required_argument, NULL, 'v' }, "N", "Verbosity level, one of 0 (None, default), 1 (Info), 2 (Debug)" },
|
||||
|
||||
{ { "help", no_argument, NULL, 'h' }, NULL, "Display this message and exit" },
|
||||
{ { "version", no_argument, NULL, 'V' }, NULL, "Display version information" },
|
||||
|
||||
{ { NULL, 0, NULL, '\0' }, NULL, NULL },
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
static size_t posArgCount = ARRAY_COUNT(optInfo);
|
||||
static size_t optCount = ARRAY_COUNT(optInfo);
|
||||
static struct option longOptions[ARRAY_COUNT(optInfo)];
|
||||
|
||||
void ConstructLongOpts() {
|
||||
size_t i;
|
||||
|
||||
for (i = 0; i < optCount; i++) {
|
||||
longOptions[i] = optInfo[i].longOpt;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int opt;
|
||||
int inputFilesCount;
|
||||
FILE** inputFiles;
|
||||
FILE* outputFile = stdout;
|
||||
char* outputFileName;
|
||||
char* dependencyFileName = NULL;
|
||||
char* ovlName = NULL;
|
||||
|
||||
ConstructLongOpts();
|
||||
|
||||
if (argc < 2) {
|
||||
printf(USAGE_STRING, argv[0]);
|
||||
fprintf(stderr, "No input file specified\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
while (true) {
|
||||
int optionIndex = 0;
|
||||
|
||||
if ((opt = getopt_long(argc, argv, OPTSTR, longOptions, &optionIndex)) == -1) {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (opt) {
|
||||
case 'M':
|
||||
dependencyFileName = optarg;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
ovlName = optarg;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
outputFileName = optarg;
|
||||
outputFile = fopen(optarg, "wb");
|
||||
if (outputFile == NULL) {
|
||||
fprintf(stderr, "error: unable to open output file '%s' for writing", optarg);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
if (sscanf(optarg, "%u", &gVerbosity) == 0) {
|
||||
fprintf(stderr, "warning: verbosity argument '%s' should be a nonnegative decimal integer", optarg);
|
||||
}
|
||||
break;
|
||||
|
||||
case 'h':
|
||||
printf(USAGE_STRING, argv[0]);
|
||||
Help_PrintHelp(HELP_PROLOGUE, posArgCount, posArgInfo, optCount, optInfo, HELP_EPILOGUE);
|
||||
return EXIT_FAILURE;
|
||||
|
||||
case 'V':
|
||||
PrintVersion();
|
||||
return EXIT_FAILURE;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "?? getopt returned character code 0x%X ??\n", opt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
FAIRY_INFO_PRINTF("%s", "Options processed\n");
|
||||
|
||||
{
|
||||
int i;
|
||||
|
||||
inputFilesCount = argc - optind;
|
||||
if (inputFilesCount == 0) {
|
||||
fprintf(stderr, "No input files specified. Exiting.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
inputFiles = malloc(inputFilesCount * sizeof(FILE*));
|
||||
for (i = 0; i < inputFilesCount; i++) {
|
||||
FAIRY_INFO_PRINTF("Using input file %s\n", argv[optind + i]);
|
||||
inputFiles[i] = fopen(argv[optind + i], "rb");
|
||||
if (inputFiles[i] == NULL) {
|
||||
fprintf(stderr, "error: unable to open input file '%s' for reading", argv[optind + i]);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
FAIRY_INFO_PRINTF("Found %d input file%s\n", inputFilesCount, (inputFilesCount == 1 ? "" : "s"));
|
||||
|
||||
if (ovlName == NULL) { // If a name has not been set using an arg
|
||||
ovlName = GetOverlayNameFromFilename(argv[optind]);
|
||||
Fado_Relocs(outputFile, inputFilesCount, inputFiles, ovlName);
|
||||
free(ovlName);
|
||||
} else {
|
||||
Fado_Relocs(outputFile, inputFilesCount, inputFiles, ovlName);
|
||||
}
|
||||
|
||||
for (i = 0; i < inputFilesCount; i++) {
|
||||
fclose(inputFiles[i]);
|
||||
}
|
||||
free(inputFiles);
|
||||
if (outputFile != stdout) {
|
||||
fclose(outputFile);
|
||||
}
|
||||
}
|
||||
|
||||
if (dependencyFileName != NULL) {
|
||||
int fileNameLength = strlen(outputFileName);
|
||||
char* objectFile = malloc((strlen(outputFileName) + 1) * sizeof(char));
|
||||
vc_vector* inputFilesVector = vc_vector_create(inputFilesCount, sizeof(char*), NULL);
|
||||
char* extensionStart;
|
||||
FILE* dependencyFile = fopen(dependencyFileName, "w");
|
||||
|
||||
if (dependencyFile == NULL) {
|
||||
fprintf(stderr, "error: unable to open dependency file '%s' for writing", dependencyFileName);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
strcpy(objectFile, outputFileName);
|
||||
extensionStart = strrchr(objectFile, '.');
|
||||
if (extensionStart == objectFile + fileNameLength) {
|
||||
fprintf(stderr, "error: file name should not end in a '.'");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
strcpy(extensionStart, ".o");
|
||||
vc_vector_append(inputFilesVector, &argv[optind], inputFilesCount);
|
||||
|
||||
Mido_WriteDependencyFile(dependencyFile, objectFile, inputFilesVector);
|
||||
|
||||
free(objectFile);
|
||||
vc_vector_release(inputFilesVector);
|
||||
fclose(dependencyFile);
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue