+ tSize = (GetLong(ptr + 4) + secalign) & ~secalign;
+ dSize = (GetLong(ptr + 8) + secalign) & ~secalign;
+ bSize = (GetLong(ptr + 12) + secalign) & ~secalign;
+ close(fd);
+ }
+
+ // Now add this image to the list of pending ofiles (plist)
+ return AddToProcessingList(ptr, fname, nullStr, 0, tSize, dSize, bSize);
+}
+
+uint32_t SymTypeAlcToAout(uint32_t alcType)
+{
+ uint32_t type = T_UNDF;
+
+ // Symbol type mappings here are derived from comparing Alcyon and BSD
+ // object files generated by MADMAC using the "size" utility from jag_utils
+ // (https://github.com/cubanismo/jag_utils) and the old PC/DOS Atari SDK.
+ //
+ // In BSD/a.out:
+ //
+ // 1) text | global text == text relocatable symbol in this file
+ // 2) data | global data == data relocatable symbol in this file
+ // 3) BSS | global BSS == bss relocatable symbol in this file
+ // 4) ABS | global ABS == non-relocatable symbol in this file
+ // 4) <none> | global <none> == undefined global symbol (extern)
+ //
+ // In DRI/Alcyon:
+ //
+ // 1) Everything seems to be marked defined.
+ // 2) There is an explicit "external" bit. It appears to be mutually
+ // exclusive with the "global" bit, at least in MADMAC's output.
+ // 3) There are separate "equated" and "equated reg" type bits that
+ // both represent ABS/non-relocatable values.
+ if ((alcType & ALCSYM_EQUATED) ||
+ (alcType & ALCSYM_EQUATED_REG))
+ type |= T_ABS;
+ else if (alcType & ALCSYM_TEXT)
+ type |= T_TEXT;
+ else if (alcType & ALCSYM_DATA)
+ type |= T_DATA;
+ else if (alcType & ALCSYM_BSS)
+ type |= T_BSS;
+
+ if ((alcType & ALCSYM_GLOBAL) ||
+ (alcType & ALCSYM_EXTERN))
+ type |= T_GLBL;
+
+ return type;
+}
+
+int LoadAlcyon(char * fname, int fd)
+{
+ char *ptr, *sptr, *aout, *saout, *traout, *strPtr;
+ char *trelptr, *drelptr, *relend;
+ struct ALCHEADER hdr;
+ struct ALCSYM *alcSyms;
+ long size = FileSize(fd);
+ size_t symStrLen;
+ size_t strOff;
+ uint32_t numSyms, numTRel, numDRel, i, j;
+
+ // Validate the file is at least large enough to contain a valid header
+ if (size < 0x1c)
+ {
+ printf("Alcyon object file %s too small to contain header\n", fname);
+ return 1;
+ }
+
+ // Allocate memory for file data
+ ptr = malloc(size);
+
+ if (ptr == NULL)
+ {
+ printf("Out of memory while processing %s\n", fname);
+ close(fd);
+ return 1;
+ }
+
+ // Read in file data
+ if (read(fd, ptr, size) != size)
+ {
+ printf("File read error on %s\n", fname);
+ close(fd);
+ free(ptr);
+ return 1;
+ }
+
+ close(fd);
+
+ hdr.magic = GetWord(ptr);
+ hdr.tsize = GetLong(ptr + 2);
+ hdr.dsize = GetLong(ptr + 6);
+ hdr.bsize = GetLong(ptr + 10);
+ hdr.ssize = GetLong(ptr + 14);
+
+ // Construct a BSD-style/aout object file in memory from the Alcyon data
+ numSyms = hdr.ssize / 14;
+
+ alcSyms = calloc(numSyms, sizeof(*alcSyms));
+ if (alcSyms == NULL)
+ {
+ printf("Out of memory while processing %s\n", fname);
+ free(ptr);
+ return 1;
+ }
+
+ 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++;
+ }