diff options
author | Akira Akashi <rubenru09@aol.com> | 2021-06-02 22:13:57 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-02 22:13:57 +0100 |
commit | 9d3d4a2acff67f43896e9e0dcd26e7aaeb55e3b3 (patch) | |
tree | d707aa8fead427dadbbb29b886875e6c8961bd5a /tools/o2narc/o2narc.cpp | |
parent | a2a17a9426f16ee601ff17840b8bb7851fef7138 (diff) | |
parent | 7e32d3758e7e36d7a67a1442cdb9386f9aa18a6e (diff) |
Merge branch 'master' into 0202A1E0
Diffstat (limited to 'tools/o2narc/o2narc.cpp')
-rw-r--r-- | tools/o2narc/o2narc.cpp | 64 |
1 files changed, 57 insertions, 7 deletions
diff --git a/tools/o2narc/o2narc.cpp b/tools/o2narc/o2narc.cpp index 1381788a..60e4e242 100644 --- a/tools/o2narc/o2narc.cpp +++ b/tools/o2narc/o2narc.cpp @@ -3,6 +3,7 @@ #include <getopt.h> #include <cstring> #include <vector> +#include <iomanip> #include "elf.h" #include "Narc.h" @@ -49,6 +50,7 @@ class Elf { public: ShdrTab shdr; Symtab symtab; + Elf(string const& filename) : Elf(filename.c_str()) {} Elf(const char * filename) { // Read the ELF header phdr = nullptr; @@ -155,25 +157,29 @@ static inline void usage() { cout << "\toutfile\tOutput NARC file" << endl; cout << "Options:" << endl; cout << "\t-f|--flatten\tDon't generate NARC headers" << endl; + cout << "\t-i|--output-header\tCreate a .naix file" << endl; } int main(int argc, char ** argv) { // CLI arguments - int flatten = 0; + int flatten = 0, output_header = 0; char padding = '\xFF'; static option options [] { { "flatten", no_argument, &flatten, 1 }, { "padding", required_argument, nullptr, 'p' }, + { "output_header", no_argument, &output_header, 1 }, {nullptr, 0, nullptr, 0} }; int opt_index; int c; - while ((c = getopt_long(argc, argv, "fp:", options, &opt_index)) != -1) + while ((c = getopt_long(argc, argv, "fp:i", options, &opt_index)) != -1) { if (c == 'f') { flatten = 1; } else if (c == 'p') { padding = strtol(optarg, NULL, 0); + } else if (c == 'i') { + output_header = 1; } } argv += optind; @@ -188,8 +194,13 @@ int main(int argc, char ** argv) { cerr << "Excess arguments: first unrecognized '" << argv[2] << "'" << endl; return 1; } - char * infname = argv[0]; - char * outfname = argv[1]; + if (output_header && flatten) { + usage(); + cerr << "Incompatible flags: -i, -f" << endl; + return 1; + } + const char * infname = argv[0]; + const char * outfname = argv[1]; // Read the ELF file Elf elf(infname); @@ -201,15 +212,45 @@ int main(int argc, char ** argv) { exit(1); } - fstream ofile; - ofile.open(outfname, ios_base::out | ios_base::binary); + ofstream ofile; + ofile.open(outfname, ios_base::binary); if (!ofile.good()) { cerr << "ERROR: Unable to open '" << outfname << "' for writing" << endl; exit(1); } - if (!flatten) // then build the NARC chunks { + string stem, stem_upper; + ofstream ofheader; + if (output_header) + { + string outhname = outfname; + outhname.replace(outhname.find(".narc"), 5, ".naix"); + ofheader.open(outhname); + if (!ofheader.good()) + { + ofile.close(); + cerr << "ERROR: Unable to open '" << outhname << "' for writing" << endl; + exit(1); + } + stem = outfname; + stem = stem.substr(0, stem.rfind(".")); + stem = stem.substr(stem.rfind("/") + 1); + stem_upper = stem; + for (char &_c : stem_upper) + { _c = toupper(_c); } + + ofheader << "/*\n" + " * THIS FILE WAS AUTOMATICALLY\n" + " * GENERATED BY tools/o2narc\n" + " * DO NOT MODIFY!!!\n" + " */\n" + "\n" + "#ifndef NARC_" << stem_upper << "_NAIX_\n" + "#define NARC_" << stem_upper << "_NAIX_\n" + "\n" + "enum {\n"; + } // .data contains the size table Elf32_Shdr & data_sec = elf.shdr[".data"]; uint32_t * _data = (uint32_t *)elf.read(data_sec); @@ -275,6 +316,15 @@ int main(int argc, char ** argv) { { _rodata[fat_entries[i].Start + j] = padding; } + if (output_header) + { + ofheader << "\tNARC_" << stem << "_narc_" << setw(4) << setfill('0') << i << " = " << i << ",\n"; + } + } + if (output_header) + { + ofheader << "};\n\n#endif //NARC_" << stem_upper << "_NAIX_\n"; + ofheader.close(); } // These NARCs have empty FNTs FileNameTable fnt{ |