+ uint32_t write_count = 0;
+ for (uint32_t i=0; i<8; i++) {
+ switch (i) {
+ case 0: write_count = fwrite(&(aout_exec->a_midmag), 4, 1, output); break;
+ case 1: write_count = fwrite(&(aout_exec->a_text), 4, 1, output); break;
+ case 2: write_count = fwrite(&(aout_exec->a_data), 4, 1, output); break;
+ case 3: write_count = fwrite(&(aout_exec->a_bss), 4, 1, output); break;
+ case 4: write_count = fwrite(&(aout_exec->a_syms), 4, 1, output); break;
+ case 5: write_count = fwrite(&(aout_exec->a_entry), 4, 1, output); break;
+ case 6: write_count = fwrite(&(aout_exec->a_trsize), 4, 1, output); break;
+ case 7: write_count = fwrite(&(aout_exec->a_drsize), 4, 1, output); break;
+ }
+ if (write_count != 1) {
+ fprintf(stderr, "ERROR: Failed to write aout header.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+}
+
+void
+write_aout_text_segment(FILE * output, uint32_t * text_segment)
+{
+ for (uint32_t i=1; i <= text_segment[0]; i++) {
+ uint32_t write_count = fwrite(&(text_segment[i]), 4, 1, output);
+ if (write_count != 1) {
+ fprintf(stderr, "ERROR: Failed to write text segment.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+}
+
+void
+write_aout_symbol_table(FILE * output, struct nlist * symbol_table, uint32_t symbol_count)
+{
+ uint32_t write_count = 0;
+ for (uint32_t i=0; i < symbol_count; i++) {
+ for (uint32_t j=0; j<5; j++) {
+ switch (j) {
+ case 0: write_count = fwrite(&(symbol_table[i].n_un.n_strx), 4, 1, output); break;
+ case 1: write_count = fwrite(&(symbol_table[i].n_type), 4, 1, output); break;
+ case 2: write_count = fwrite(&(symbol_table[i].n_other), 4, 1, output); break;
+ case 3: write_count = fwrite(&(symbol_table[i].n_desc), 4, 1, output); break;
+ case 4: write_count = fwrite(&(symbol_table[i].n_value), 4, 1, output); break;
+ }
+ if (write_count != 1) {
+ fprintf(stderr, "ERROR: Failed to write symbol table.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
+}
+
+void
+write_aout_string_table(FILE * output, char * string_table, uint32_t string_table_size)
+{
+ uint32_t write_count = fwrite(&string_table_size, 4, 1, output);
+ if (write_count != 1) {
+ fprintf(stderr, "ERROR: Failed to write string table size.\n");
+ exit(EXIT_FAILURE);
+ }
+ for (uint32_t i=0; i < string_table_size; i++) {
+ write_count = fwrite(&(string_table[i]), 1, 1, output);
+ if (write_count != 1) {
+ fprintf(stderr, "ERROR: Failed to write string table.\n");
+ exit(EXIT_FAILURE);
+ }
+ }