* Emit the complete patched executable binary file. */
| 37 | * Emit the complete patched executable binary file. |
| 38 | */ |
| 39 | void emitBinary(const char *filename, const uint8_t *bin, size_t len) |
| 40 | { |
| 41 | FILE *out = fopen(filename, "w"); |
| 42 | if (out == nullptr) |
| 43 | error("failed to open output file \"%s\" for writing: %s", filename, |
| 44 | strerror(errno)); |
| 45 | if (fwrite(bin, sizeof(uint8_t), len, out) != len) |
| 46 | error("failed to write output to file \"%s\": %s", filename, |
| 47 | strerror(errno)); |
| 48 | if (fchmod(fileno(out), S_IRUSR | S_IWUSR | S_IXUSR | |
| 49 | S_IRGRP | S_IWGRP | S_IXGRP | |
| 50 | S_IROTH | S_IWOTH | S_IXOTH)) |
| 51 | { |
| 52 | // This is a warning since the output might be /dev/null |
| 53 | warning("failed to set execute permission for output file \"%s\": %s", |
| 54 | filename, strerror(errno)); |
| 55 | } |
| 56 | if (fclose(out) < 0) |
| 57 | error("failed to close output file \"%s\": %s", filename, |
| 58 | strerror(errno)); |
| 59 | } |
| 60 | |
| 61 | /* |
| 62 | * Emit a binary patch. Implements (an approximation of) the pipeline: |