relocmain/manip-exe.c

67 lines
1.3 KiB
C

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
// TODO: allow for non-native-bitness ELF parsing
#include "manip-exe.h"
int main(int argc, char* argv[]) {
if (argc < 4) {
fprintf(stderr,"usage: %s <input> <output> <symbol>\n", argv[0]);
return 0;
}
const char* symname = argv[3];
FILE* f = fopen(argv[1], "rb");
if (f == NULL) {
fprintf(stderr, "%s: can't open input file '%s'\n", argv[0], argv[1]);
return 1;
}
fseek(f, 0, SEEK_END);
long s = ftell(f);
fseek(f, 0, SEEK_SET);
fprintf(stderr, "s=%zu\n", s);
void* blob = calloc(1, (size_t)s);
if (fread(blob, 1, (size_t)s, f) != (size_t)s) {
fprintf(stderr, "%s: can't read input file '%s'\n", argv[0], argv[1]);
free(blob);
fclose(f);
return 1;
}
fclose(f);
if (manip_elf(blob, symname)) {
fprintf(stderr, "%s: ELF patching failed\n", argv[0]);
free(blob);
return 1;
}
f = fopen(argv[2], "wb");
if (f == NULL) {
fprintf(stderr, "%s: can't open output file '%s'\n", argv[0], argv[2]);
return 1;
}
if (fwrite(blob, 1, (size_t)s, f) != (size_t)s) {
fprintf(stderr, "%s: can't write output file '%s'\n", argv[0], argv[2]);
free(blob);
fclose(f);
return 1;
}
fclose(f);
free(blob);
return 0;
}