* © 2021 Aaron Taylor <ataylor at subgeniuskitty dot com>
* See LICENSE.txt file for copyright and license details.
* This program dumps the text segment of a NED1 a.out format executable as a
#include "../common/a.out.h"
print_usage(char ** argv
)
printf( "NED objdump v%d (www.subgeniuskitty.com)\n"
"Usage: %s -i <file> -o <file>\n"
" -h Help (prints this message)\n"
" -i <file> Specify NED a.out format input file.\n"
" -o <file> Specify output filename for binary blob.\n"
die(FILE * in
, FILE * out
, const char * msg
)
fprintf(stderr
, "%s\n", msg
);
dump_aout_file(FILE * in_file
, FILE * out_file
)
/* Read in and check the a.out header. */
for (uint32_t i
=0; i
<8; i
++) {
case 0: read_count
= fread(&(aout_exec
.a_midmag
), 4, 1, in_file
); break;
case 1: read_count
= fread(&(aout_exec
.a_text
), 4, 1, in_file
); break;
case 2: read_count
= fread(&(aout_exec
.a_data
), 4, 1, in_file
); break;
case 3: read_count
= fread(&(aout_exec
.a_bss
), 4, 1, in_file
); break;
case 4: read_count
= fread(&(aout_exec
.a_syms
), 4, 1, in_file
); break;
case 5: read_count
= fread(&(aout_exec
.a_entry
), 4, 1, in_file
); break;
case 6: read_count
= fread(&(aout_exec
.a_trsize
), 4, 1, in_file
); break;
case 7: read_count
= fread(&(aout_exec
.a_drsize
), 4, 1, in_file
); break;
die(in_file
, out_file
, "ERROR: Invalid a.out header.");
if (N_BADMAG(aout_exec
)) {
die(in_file
, out_file
, "ERROR: Invalid magic number in a.out header.");
} else if (N_GETMID(aout_exec
) != MID_NED
) {
die(in_file
, out_file
, "ERROR: Executable not intended for NED Machine ID.");
/* Read in the text segment. */
uint32_t text_segment_size
= (N_DATOFF(aout_exec
) - N_TXTOFF(aout_exec
));
uint8_t * text_segment
= malloc(text_segment_size
);
read_count
= fread(text_segment
, 1, text_segment_size
, in_file
);
if (read_count
!= text_segment_size
) {
die(in_file
, out_file
, "ERROR: Failed to read entire text segment.");
/* Correct the byte order. */
for (uint32_t i
=0; i
< (text_segment_size
/ 4); i
++) {
for (uint8_t j
=0; j
<4; j
++) temp_word
[j
] = text_segment
[((i
*4)+j
)];
for (uint8_t j
=0; j
<4; j
++) text_segment
[((i
*4)+j
)] = temp_word
[(3-j
)];
/* Write text segment to file. */
uint32_t write_count
= fwrite(text_segment
, text_segment_size
, 1, out_file
);
die(in_file
, out_file
, "ERROR: Unable to write to output file.");
/* Clean up and return */
main(int argc
, char ** argv
)
* Process command line arguments
while ((c
= getopt(argc
, argv
, "hi:o:")) != -1) {
if ((in_file
= fopen(optarg
, "r")) == NULL
) {
fprintf(stderr
, "ERROR: %s: %s\n", optarg
, strerror(errno
));
if ((out_file
= fopen(optarg
, "wx")) == NULL
) {
fprintf(stderr
, "ERROR: %s: %s\n", optarg
, strerror(errno
));
fprintf(stderr
, "ERROR: Must specify NED1 a.out format file with -i flag.\n");
fprintf(stderr
, "ERROR: Must specify destination file with -o flag.\n");
dump_aout_file(in_file
, out_file
);