+
+ /* Read in the symbol table. */
+ *symbol_count = ((N_STROFF(*aout_exec) - N_SYMOFF(*aout_exec)) / 20); /* 20 bytes per symbol. */
+ *symbol_table = malloc((*symbol_count) * sizeof(struct nlist));
+ for (uint32_t i=0; i < *symbol_count; i++) {
+ for (uint32_t j=0; j<5; j++) {
+ switch (j) {
+ case 0: read_count = fread(&((*symbol_table)[i].n_un.n_strx), 4, 1, input); break;
+ case 1: read_count = fread(&((*symbol_table)[i].n_type), 4, 1, input); break;
+ case 2: read_count = fread(&((*symbol_table)[i].n_other), 4, 1, input); break;
+ case 3: read_count = fread(&((*symbol_table)[i].n_desc), 4, 1, input); break;
+ case 4: read_count = fread(&((*symbol_table)[i].n_value), 4, 1, input); break;
+ }
+ if (read_count != 1) {
+ fprintf(stderr, "ERROR: Unable to read entire symbol table.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+ }
+
+ /* Read in the string table and update the symbol table entries with pointers to new strings. */
+ uint32_t string_table_size;
+ read_count = fread(&string_table_size, 4, 1, input);
+ if (read_count != 1) {
+ fprintf(stderr, "ERROR: Failed to read string table size.\n");
+ exit(EXIT_FAILURE);
+ }
+ for (uint32_t i=0; i < *symbol_count; i++) {
+ uint32_t len = 0;
+ if (i < ((*symbol_count)-1)) {
+ len = ((*symbol_table)[i+1].n_un.n_strx - (*symbol_table)[i].n_un.n_strx);
+ } else {
+ len = (string_table_size - (*symbol_table)[i].n_un.n_strx);
+ }
+ (*symbol_table)[i].n_un.n_name = malloc(len);
+ read_count = fread((*symbol_table)[i].n_un.n_name, 1, len, input);
+ if (read_count != len) {
+ fprintf(stderr, "ERROR: Failed to read a string from the string table.\n");
+ exit(EXIT_FAILURE);
+ }
+ }
+