+ }
+ }
+ else if (obj_format == ALCYON)
+ {
+ if (verb_flag)
+ {
+ if (prg_flag)
+ {
+ printf("TOS header : 28 bytes\n");
+ printf("Total : %d bytes\n", 28 + sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc);
+ }
+ else
+ {
+ printf("Total : %d bytes\n", sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc);
+ }
+ }
+
+ // Compute size of symbol table; assign numbers to the symbols...
+ ssize = 0;
+
+ // As we grabbed BSD *and* Alcyon in prg_flag == 0 mode, this is *always*
+ // false... :-P
+ if (prg_flag != 1)
+ ssize = sy_assign(NULL, NULL) * 14;
+
+ // Alloc memory for header + text + data, symbol and relocation
+ // information construction.
+ t = tds = sect[TEXT].sloc + sect[DATA].sloc;
+
+ if (t < ssize)
+ t = ssize;
+
+ // Is there any reason to do this this way???
+ buf = malloc(t + HDRSIZE);
+ buf += HDRSIZE;
+
+ // Build object file header just before the text+data image
+ chptr = buf - HDRSIZE; // -> base of header
+ D_word(0x601A); // 00 - magic number
+ D_long(sect[TEXT].sloc); // 02 - TEXT size
+ D_long(sect[DATA].sloc); // 06 - DATA size
+ D_long(sect[BSS].sloc); // 0A - BSS size
+ D_long(ssize); // 0E - symbol table size
+ D_long(0); // 12 - stack size (unused)
+ D_long(PRGFLAGS); // 16 - PRGFLAGS
+ D_word(0); // 1A - relocation information exists
+
+ // Construct text and data segments; fixup relocatable longs in .PRG
+ // mode; finally write the header + text + data
+ p = buf;
+
+ for(i=TEXT; i<=DATA; i++)
+ {
+ for(cp=sect[i].sfcode; cp!=NULL; cp=cp->chnext)
+ {
+ memcpy(p, cp->chptr, cp->ch_size);
+ p += cp->ch_size;
+ }
+ }
+
+ // Do a first pass on the Alcyon image, if in PRG mode
+ if (prg_flag)
+ MarkImage(buf, tds, sect[TEXT].sloc, 0);
+
+ unused = write(fd, buf - HDRSIZE, tds + HDRSIZE);
+
+ // Construct and write symbol table
+ if (prg_flag != 1)
+ {
+ sy_assign(buf, AddSymEntry);
+ unused = write(fd, buf, ssize);
+ }
+
+ // Construct and write relocation information; the size of it changes if
+ // we're writing a RELMODed executable.
+ tds = MarkImage(buf, tds, sect[TEXT].sloc, 1);
+ unused = write(fd, buf, tds);
+ }
+ else if (obj_format == ELF)
+ {
+ // Allocate 6MB object file image memory
+ buf = malloc(0x600000);
+
+ if (buf == NULL)
+ {
+ error("cannot allocate object file memory (in BSD mode)");
+ return ERROR;
+ }
+
+ memset(buf, 0, 0x600000);
+ objImage = buf; // Set global object image pointer
+ strtable = malloc(0x200000); // Allocate 2MB string table buffer
+
+ if (strtable == NULL)
+ {
+ error("cannot allocate string table memory (in BSD mode)");
+ return ERROR;
+ }
+
+ memset(strtable, 0, 0x200000);
+
+ // This is pretty much a first pass at this shite, so there's room for
+ // improvement. :-P
+ uint8_t headers[4 * 10 * 10]; // (DWORD * 10) = 1 hdr, 10 entries
+ int headerSize = 0;
+ uint8_t shstrtab[128]; // The section header string table proper
+ uint32_t shstTab[9]; // Index into shstrtab for strings
+ uint8_t * shstPtr = shstrtab; // Temp pointer
+ uint32_t shstSize = 0;
+ int numEntries = 4; // There are always at *least* 4 sections
+ int shstIndex = 1; // The section where the shstrtab lives
+ int elfSize = 0; // Size of the ELF object
+ // Clear the header numbers
+ memset(elfHdrNum, 0, 9 * sizeof(int));
+
+ //
+ // First step is to see what sections need to be made; we also
+ // construct the section header string table here at the same time.
+ //
+ shstTab[ES_NULL] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, "");
+ shstTab[ES_SHSTRTAB] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, ".shstrtab");
+ shstTab[ES_SYMTAB] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, ".symtab");
+ shstTab[ES_STRTAB] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, ".strtab");
+
+ if (sect[TEXT].sloc > 0)
+ {
+ elfHdrNum[ES_TEXT] = shstIndex;
+ shstTab[ES_TEXT] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, "TEXT");
+ shstIndex++;
+ numEntries++;
+ }
+
+ if (sect[DATA].sloc > 0)
+ {
+ elfHdrNum[ES_DATA] = shstIndex;
+ shstTab[ES_DATA] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, "DATA");
+ shstIndex++;
+ numEntries++;
+ }
+
+ if (sect[BSS].sloc > 0)
+ {
+ elfHdrNum[ES_BSS] = shstIndex;
+ shstTab[ES_BSS] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, "BSS");
+ shstIndex++;
+ numEntries++;
+ }
+
+ if (sect[TEXT].relocs > 0)
+ {
+ elfHdrNum[ES_RELATEXT] = shstIndex;
+ shstTab[ES_RELATEXT] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, ".relaTEXT");
+ shstIndex++;
+ numEntries++;
+ }
+
+ if (sect[DATA].relocs > 0)
+ {
+ elfHdrNum[ES_RELADATA] = shstIndex;
+ shstTab[ES_RELADATA] = shstSize;
+ shstSize += DepositELFSHSTEntry(&shstPtr, ".relaDATA");
+ shstIndex++;
+ numEntries++;
+ }
+
+ elfHdrNum[ES_SHSTRTAB] = shstIndex + 0;
+ elfHdrNum[ES_SYMTAB] = shstIndex + 1;
+ elfHdrNum[ES_STRTAB] = shstIndex + 2;
+
+#ifdef DEBUG_ELF
+printf("ELF shstrtab size: %i bytes. Entries:\n", shstSize);
+for(int j=0; j<i; j++)
+ printf("\"%s\"\n", shstrtab + shstTab[j]);
+#endif
+
+ // Construct ELF header
+ // If you want to make any sense out of this you'd better take a look
+ // at Executable and Linkable Format on Wikipedia.
+ chptr = buf;
+ D_long(0x7F454C46); // 00 - "<7F>ELF" Magic Number
+ D_byte(0x01); // 04 - 32 vs 64 (1 = 32, 2 = 64)
+ D_byte(0x02); // 05 - Endianness (1 = LE, 2 = BE)
+ D_byte(0x01); // 06 - Original version of ELF (set to 1)
+ D_byte(0x00); // 07 - Target OS ABI (0 = System V)
+ D_byte(0x00); // 08 - ABI Extra (unneeded)
+ D_byte(0x00); // 09 - Pad bytes
+ D_word(0x00);
+ D_long(0x00);
+ D_word(0x01); // 10 - ELF Type (1 = relocatable)
+ D_word(0x04); // 12 - Architecture (EM_68K = 4, Motorola M68K family)
+ D_long(0x01); // 14 - Version (1 = original ELF)
+ D_long(0x00); // 18 - Entry point virtual address (unneeded)
+ D_long(0x00); // 1C - Program header table offset (unneeded)
+ D_long(0x00); // 20 - Section header table offset (to be determined)
+
+ if (0)
+ {
+ // Specifically for 68000 CPU
+ D_long(0x01000000) // 24 - Processor-specific flags - EF_M68K_M68000
+ }
+ else
+ {
+ // CPUs other than 68000 (68020...)
+ D_long(0); // 24 - Processor-specific flags (ISA dependent)
+ }
+
+ D_word(0x0034); // 28 - ELF header size in bytes
+ D_word(0); // 2A - Program header table entry size
+ D_word(0); // 2C - Program header table entry count
+ D_word(0x0028); // 2E - Section header entry size - 40 bytes for ELF32
+ D_word(numEntries); // 30 - Section header table entry count
+ D_word(shstIndex); // 32 - Section header string table index
+
+ elfSize += 0x34;
+
+ // Deposit section header 0 (NULL)
+ headerSize += DepositELFSectionHeader(headers + headerSize, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
+
+ int textLoc = elfSize;