+ // Build object file header just before the text+data image
+ chptr = buf; // -> base of header
+ ch_size = 0;
+ challoc = HDRSIZE + tds + symbolMaxSize;
+ 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(0); // 0E - symbol table size (filled later)
+ 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 + HDRSIZE;
+
+ 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 + HDRSIZE, tds, sect[TEXT].sloc, 0);
+
+ // Construct symbol table and update the header entry, if necessary
+ if (prg_flag > 1)
+ {
+ // sy_assign with AddSymEntry updates symsize (stays 0 otherwise)
+ sy_assign(buf + HDRSIZE + tds, AddSymEntry);
+ chptr = buf + 0x0E; // Point to symbol table size entry
+ D_long(symsize);
+
+ if (verb_flag)
+ printf("Symbol table: %d bytes\n", symsize);
+ }
+
+ // Write out the header + text & data + symbol table (if any)
+ unused = write(fd, buf, HDRSIZE + tds + symsize);
+
+ // Construct and write relocation information; the size of it changes if
+ // we're writing a RELMODed executable. N.B.: Destroys buffer!
+ 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 ELF 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 ELF 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;
+ ch_size = 0;
+ challoc = 0x600000;
+ 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;
+
+ // Construct TEXT section, if any
+ if (sect[TEXT].sloc > 0)
+ {
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_TEXT], 1, 6, 0, elfSize, sect[TEXT].sloc, 0, 0, largestAlign[0], 0);
+
+ for(CHUNK * cp=sect[TEXT].sfcode; cp!=NULL; cp=cp->chnext)
+ {
+ memcpy(buf + elfSize, cp->chptr, cp->ch_size);
+ elfSize += cp->ch_size;
+ }
+
+ // Pad for next section (LONG boundary)
+ elfSize = (elfSize + 3) & ~3;
+ }
+
+ int dataLoc = elfSize;
+
+ // Construct DATA section, if any
+ if (sect[DATA].sloc > 0)
+ {
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_DATA], 1, 3, 0, elfSize, sect[DATA].sloc, 0, 0, largestAlign[1], 0);
+
+ for(CHUNK * cp=sect[DATA].sfcode; cp!=NULL; cp=cp->chnext)
+ {
+ memcpy(buf + elfSize, cp->chptr, cp->ch_size);
+ elfSize += cp->ch_size;
+ }
+
+ // Pad for next section (LONG boundary)
+ elfSize = (elfSize + 3) & ~3;
+ }
+
+ // Construct BSS section, if any
+ if (sect[BSS].sloc > 0)
+ {
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_BSS], 8, 3, 0, elfSize, sect[BSS].sloc, 0, 0, largestAlign[2], 0);
+ }
+
+ int textrelLoc = headerSize;
+
+ // Add headers for relocated sections, if any...
+ if (sect[TEXT].relocs > 0)
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_RELATEXT], 4, 0x00, 0, 0, 0, elfHdrNum[ES_SYMTAB], elfHdrNum[ES_TEXT], 4, 0x0C);
+
+ int datarelLoc = headerSize;
+
+ if (sect[DATA].relocs > 0)
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_RELADATA], 4, 0x40, 0, 0, 0, elfHdrNum[ES_SYMTAB], elfHdrNum[ES_DATA], 4, 0x0C);
+
+ // Add shstrtab
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_SHSTRTAB], 3, 0, 0, elfSize, shstSize, 0, 0, 1, 0);
+ memcpy(buf + elfSize, shstrtab, shstSize);
+ elfSize += shstSize;
+ // Pad for next section (LONG boundary)
+ elfSize = (elfSize + 3) & ~3;
+
+ // Add section headers
+ int headerLoc = elfSize;
+ chptr = buf + 0x20; // Set section header offset in ELF header
+ D_long(headerLoc);
+ elfSize += (4 * 10) * numEntries;
+
+ // Add symbol table & string table
+ int symtabLoc = elfSize;
+ strindx = 0; // Make sure we start at the beginning...
+ elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 0, 0, 0);
+ *strtable = 0;
+ strindx++;
+ extraSyms = 1;
+
+ if (sect[TEXT].sloc > 0)
+ {
+ elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_TEXT]);
+ extraSyms++;
+ }
+
+ if (sect[DATA].sloc > 0)
+ {
+ elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_DATA]);
+ extraSyms++;
+ }
+
+ if (sect[BSS].sloc > 0)
+ {
+ elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_BSS]);
+ extraSyms++;
+ }
+
+ int numSymbols = sy_assign_ELF(buf + elfSize, AddELFSymEntry);
+ elfSize += numSymbols * 0x10;
+
+ // String table
+ int strtabLoc = elfSize;
+ memcpy(buf + elfSize, strtable, strindx);
+ elfSize += strindx;
+ // Pad for next section (LONG boundary)
+ elfSize = (elfSize + 3) & ~3;
+
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_SYMTAB], 2, 0, 0, symtabLoc, (numSymbols + extraSyms) * 0x10, shstIndex + 2, firstglobal + extraSyms, 4, 0x10);
+ headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_STRTAB], 3, 0, 0, strtabLoc, strindx, 0, 0, 1, 0);
+
+ // Add relocation tables, if any (no need to align after these, they're
+ // already on DWORD boundaries)
+ if (sect[TEXT].relocs > 0)
+ {
+ uint32_t textrelSize = CreateELFRelocationRecord(buf + elfSize, buf + textLoc, TEXT);
+ // Deposit offset & size, now that we know them
+ chptr = headers + textrelLoc + 0x10;
+ D_long(elfSize);
+ D_long(textrelSize);
+ elfSize += textrelSize;
+ }
+
+ if (sect[DATA].relocs > 0)
+ {
+ uint32_t datarelSize = CreateELFRelocationRecord(buf + elfSize, buf + dataLoc, DATA);
+ // Deposit offset & size, now that we know them
+ chptr = headers + datarelLoc + 0x10;
+ D_long(elfSize);
+ D_long(datarelSize);
+ elfSize += datarelSize;
+ }
+
+ // Copy headers into the object
+ memcpy(buf + headerLoc, headers, headerSize);
+
+ // Finally, write out the object
+ unused = write(fd, buf, elfSize);
+
+ // Free allocated memory
+ if (buf)
+ {
+ free(buf);
+ free(strtable);
+ }
+ }
+ else if (obj_format == XEX)
+ {
+ // Just write the object file
+ m6502obj(fd);
+ }
+ else if (obj_format == P56 || obj_format == LOD)
+ {
+ // Allocate 6MB object file image memory
+ uint8_t * buf = malloc(0x600000);
+
+ if (buf == NULL)
+ return error("cannot allocate object file memory (in P56/LOD mode)");
+
+// objImage = buf; // Set global object image pointer
+
+ memset(buf, 0, 0x600000); // Clear allocated memory
+
+ // Iterate through DSP ram buffers
+ chptr = buf; // -> base of header
+ ch_size = 0;
+ challoc = 0x600000;
+
+ if (obj_format == LOD)
+ WriteLOD();
+ else
+ WriteP56();
+
+ // Write all the things |o/
+ unused = write(fd, buf, chptr - buf);
+
+ if (buf)
+ free(buf);
+ }
+
+ return 0;
+}
+
+
+static void WriteLOD(void)
+{
+ D_printf("_START %s 0000 0000 0000 RMAC %01i.%01i.%01i\n\n", firstfname, MAJOR, MINOR, PATCH);
+
+ for(DSP_ORG * l=&dsp_orgmap[0]; l<dsp_currentorg; l++)
+ {
+ if (l->end != l->start)
+ {
+ switch (l->memtype)
+ {
+ case ORG_P: D_printf("_DATA P %.4X\n", l->orgadr); break;
+ case ORG_X: D_printf("_DATA X %.4X\n", l->orgadr); break;
+ case ORG_Y: D_printf("_DATA Y %.4X\n", l->orgadr); break;
+ case ORG_L: D_printf("_DATA L %.4X\n", l->orgadr); break;
+ default:
+ error("Internal error: unknown DSP56001 org'd section");
+ return;
+ }
+
+ CHUNK * cp = l->chunk;
+ uint8_t * p_chunk = l->start;
+ uint8_t * p_chunk_end = p_chunk;
+ uint32_t j = 0;
+
+ while (p_chunk_end != l->end)
+ {
+ if (l->end < (cp->chptr + cp->ch_size) && l->end > cp->chptr)
+ {
+ // If the end of the section is inside the current chunk, just dump everything and stop
+ p_chunk_end = l->end;
+ }
+ else
+ {
+ // If the end of the section is not inside the current chunk, just dump everything from the current chunk and move on to the next
+ p_chunk_end = cp->chptr + cp->ch_size;
+ }
+
+ uint32_t count = (uint32_t)(p_chunk_end - p_chunk);
+
+ for(uint32_t i=0; i<count; i+=3)
+ {
+ if ((j & 7) != 7)
+ {
+ D_printf("%.6X ", (((p_chunk[0] << 8) | p_chunk[1]) << 8) | p_chunk[2]);
+ }
+ else
+ {
+ D_printf("%.6X\n", (((p_chunk[0] << 8) | p_chunk[1]) << 8) | p_chunk[2]);
+ }
+
+ p_chunk += 3;
+ j++;
+ }
+
+ cp = cp->chnext; // Advance chunk
+
+ if (cp != NULL)
+ p_chunk = cp->chptr; // Set dump pointer to start of this chunk
+ }
+
+ if ((j & 7) != 0)
+ D_printf("\n");
+ }
+ }
+
+ // Dump the symbol table into the buf
+ DumpLODSymbols();
+
+ D_printf("\n_END %.4X\n", dsp_orgmap[0].orgadr);
+}
+
+
+static void WriteP56(void)
+{
+ for(DSP_ORG * l=&dsp_orgmap[0]; l<dsp_currentorg; l++)
+ {
+ if (l->end == l->start)
+ continue;
+
+ if ((l->memtype < ORG_P) || (l->memtype > ORG_L))
+ {
+ error("Internal error: unknown DSP56001 org'd section");
+ return;
+ }
+
+ CHUNK * cp = l->chunk;
+ uint8_t * p_chunk = l->start;
+ uint8_t * p_chunk_end = p_chunk;
+
+ // Memory type (P, X, Y or L)
+ D_dsp(l->memtype);
+
+ // Chunk start address (in DSP words)
+ D_dsp(l->orgadr);
+
+ // Chunk length (in DSP words)
+ // We'll fill this field after we write the chunk so we can calculate
+ // how long it is (so if the chunk is split into different CHUNKs we
+ // can deal with this during copy)
+ uint8_t * p_buf_len = chptr;
+ chptr += 3;
+
+ // The chunk itself
+ uint32_t chunk_size = 0;
+
+ while (p_chunk_end != l->end)
+ {
+ if (l->end < (cp->chptr + cp->ch_size) && l->end > cp->chptr)
+ {
+ // If the end of the section is inside the current chunk, just
+ // dump everything and stop
+ p_chunk_end = l->end;
+ }
+ else
+ {
+ // If the end of the section is not inside the current chunk,
+ // just dump everything from the current chunk and move on to
+ // the next
+ p_chunk_end = cp->chptr + cp->ch_size;
+ }
+
+ uint32_t current_chunk_size = p_chunk_end - p_chunk;
+ chunk_size += current_chunk_size;
+ memcpy(chptr, p_chunk, current_chunk_size);
+ chptr += current_chunk_size;
+
+ cp = cp->chnext; // Advance chunk
+
+ if (cp != NULL)
+ p_chunk = cp->chptr; // Set dump pointer to start of this chunk
+ }
+
+ // Now we can mark the chunk's length (DSP word size is 24-bits, so
+ // the byte count needs to be divided by 3)
+ SETBE24(p_buf_len, chunk_size / 3);
+ }
+}