+ sptr = ptr + 0x1c + hdr.tsize + hdr.dsize;
+ trelptr = sptr + hdr.ssize;
+ drelptr = trelptr + hdr.tsize;
+ relend = drelptr + hdr.dsize;
+
+ if (relend - ptr > size)
+ {
+ printf("Alcyon object file %s truncated: Header wants %ldB, file is %ldB\n",
+ fname, relend - ptr, size);
+ return 1;
+ }
+
+ for (i = 0, symStrLen = 0; i < numSyms; i++)
+ {
+ memcpy(alcSyms[i].name, sptr, 8);
+ alcSyms[i].type = GetWord(sptr + 8);
+ alcSyms[i].value = GetLong(sptr + 10);
+ symStrLen += strnlen((char *)alcSyms[i].name, 8) + 1;
+ sptr += 14;
+ }
+
+ for (i = 0, numTRel = 0; trelptr + i < drelptr; i += 2)
+ {
+ uint16_t rel = GetWord(trelptr + i);
+ if ((rel != ALCREL_ABS) &&
+ (rel != ALCREL_LONG))
+ numTRel++;
+ }
+
+ for (i = 0, numDRel = 0; drelptr + i < relend; i += 2)
+ {
+ uint16_t rel = GetWord(drelptr + i);
+ if ((rel != ALCREL_ABS) &&
+ (rel != ALCREL_LONG))
+ numDRel++;
+ }
+
+ aout = malloc(32 + /* header */
+ hdr.tsize +
+ hdr.dsize +
+ numTRel * 8 + /* Text section relocations */
+ numDRel * 8 + /* Data section relocations */
+ numSyms * 12 + /* symbol table */
+ 4 + symStrLen + /* string table size + strings */
+ 4 /* NULL-terminator for file */);
+ if (aout == NULL)
+ {
+ printf("Out of memory while processing %s\n", fname);
+ free(alcSyms);
+ free(ptr);
+ return 1;
+ }
+
+ // Construct the BSD/a.out header.
+ PutLong(aout, 0x00000107); // Magic number
+
+ PutLong(aout+4, hdr.tsize); // Text size
+ PutLong(aout+8, hdr.dsize); // Data size
+ PutLong(aout+12, hdr.bsize); // BSS size
+ PutLong(aout+16, numSyms * 12); // Symbol table size
+ PutLong(aout+20, 0L); // Entry point
+
+ PutLong(aout+24, numTRel * 8); // TEXT relocation size
+ PutLong(aout+28, numDRel * 8); // DATA relocation size
+
+ // Copy the raw text and data segments
+ memcpy(aout + 32, ptr + 0x1c, hdr.tsize);
+ memcpy(aout + 32 + hdr.tsize, ptr + 0x1c + hdr.tsize, hdr.dsize);
+
+ // Set traout to the start of the relocation tables
+ traout = aout + 32 + hdr.tsize + hdr.dsize;
+
+ // Set saout to symbol table location
+ saout = traout + numTRel * 8 + numDRel * 8 ;
+
+ // Convert the text and data relocations to a.out format
+ for (i = 0; trelptr + i < relend; i += 2)
+ {
+ uint16_t rel = GetWord(trelptr + i);
+ uint16_t relFlags = rel & 7;
+ uint32_t aoutflags = BSDREL_ABS;
+ uint32_t valoffset = 0;
+ char *const valaddr = aout + 32 + i;
+ const uint32_t reladdr = (trelptr + i >= drelptr) ? i - hdr.tsize : i;
+
+ if (relFlags == ALCREL_LONG)
+ {
+ i += 2;
+ rel = GetWord(trelptr + i);
+ relFlags = rel & 7;
+ }
+ else
+ {
+ aoutflags |= BSDREL_WORD;
+ }
+
+ if (relFlags == ALCREL_ABS)
+ continue;
+
+ switch (relFlags) {
+ case ALCREL_EXTPCREL:
+ aoutflags &= ~BSDREL_ABS;
+ aoutflags |= BSDREL_PCREL;
+ /* Fall through */
+ case ALCREL_EXTABS:
+ aoutflags |= BSDREL_GLOBAL;
+ aoutflags |= (ALCREL_SYMIDX(rel) << BSDREL_SYMIDX_SHIFT);
+ break;
+ case ALCREL_TEXT:
+ aoutflags |= BSDREL_SEG_TEXT;
+ break;
+ case ALCREL_DATA:
+ aoutflags |= BSDREL_SEG_DATA;
+ valoffset = hdr.tsize;
+ break;
+ case ALCREL_BSS:
+ aoutflags |= BSDREL_SEG_BSS;
+ valoffset = hdr.tsize + hdr.dsize;
+ break;
+
+ default:
+ printf("Invalid Alcyon relocation flags: 0x%02x\n", relFlags);
+ free(alcSyms);
+ free(ptr);
+ free(aout);
+ return 1;
+ }
+
+ if (valoffset != 0)
+ {
+ if (aoutflags & BSDREL_WORD)
+ {
+ valoffset += GetWord(valaddr);
+ PutWord(valaddr, (uint16_t)valoffset);
+ }
+ else
+ {
+ valoffset += GetLong(valaddr);
+ PutLong(valaddr, valoffset);
+ }
+ }
+
+ PutLong(traout, reladdr);
+ PutLong(traout+4, aoutflags);
+ traout += 8;
+ }
+
+ // Done with the Alcyon data.
+ free(ptr);
+ ptr = NULL;
+ sptr = NULL;
+
+ // Set strPtr to string table location and write string table size
+ strPtr = saout + numSyms * 12;
+ PutLong(strPtr, 4 + symStrLen);
+
+ for (i = 0, strOff = 4; i < numSyms; i++)
+ {
+ PutLong(saout, strOff); // String offset of symbol
+ PutLong(saout+4, SymTypeAlcToAout(alcSyms[i].type)); // Symbol type
+ PutLong(saout+8, alcSyms[i].value); // Symbol value
+ saout += 12;
+
+ for (j = 0; j < 8 && alcSyms[i].name[j] != '\0'; j++)
+ *(strPtr + strOff + j) = alcSyms[i].name[j];
+ strOff += j; // Step past string
+ *(strPtr + strOff) = '\0'; // Terminate symbol string
+ strOff++; // Step past termination
+ }
+
+ PutLong(strPtr + strOff, 0L); // Terminating long for object file
+
+ // Done with the Alcyon symbol table.
+ free(alcSyms);
+