2 // RMAC - Reboot's Macro Assembler for all Atari computers
3 // OBJECT.C - Writing Object Files
4 // Copyright (C) 199x Landon Dyer, 2011-2017 Reboot and Friends
5 // RMAC derived from MADMAC v1.07 Written by Landon Dyer, 1986
6 // Source utilised with the kind permission of Landon Dyer
20 uint32_t symsize = 0; // Size of BSD/ELF symbol table
21 uint32_t strindx = 0x00000004; // BSD/ELF string table index
22 uint8_t * strtable; // Pointer to the symbol string table
23 uint8_t * objImage; // Global object image pointer
24 int elfHdrNum[9] = { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
27 static uint16_t tdb_tab[] = {
29 AL_TEXT, // TEXT segment based
30 AL_DATA, 0, // DATA segment based
31 AL_BSS // BSS segment based
34 uint32_t PRGFLAGS; /* PRGFLAGS as defined in Atari Compendium Chapter 2
35 Definition Bit(s) Meaning
36 --------------- ------- --------------------------------------------------------
37 PF_FASTLOAD 0 If set, clear only the BSS area on program load,
38 otherwise clear the entire heap.
39 PF_TTRAMLOAD 1 If set, the program may be loaded into alternative RAM,
40 otherwise it must be loaded into standard RAM.
41 PF_TTRAMMEM 2 If set, the program's Malloc() requests may be satisfied
42 from alternative RAM, otherwise they must be satisfied
45 See left. 4 & 5 If these bits are set to 0 (PF_PRIVATE), the processes'
46 entire memory space will be considered private
47 (when memory protection is enabled).If these bits are
48 set to 1 (PF_GLOBAL), the processes' entire memory space
49 will be readable and writable by any process (i.e.
50 global). If these bits are set to 2 (PF_SUPERVISOR), the
51 processes' entire memory space will only be readable and
52 writable by itself and any other process in supervisor
53 mode.If these bits are set to 3 (PF_READABLE), the
54 processes' entire memory space will be readable by any
55 application but only writable by itself.
56 - 6-15 Currently unused
61 // Add entry to symbol table (in ALCYON mode)
62 // If 'globflag' is 1, make the symbol global
63 // If in .PRG mode, adjust symbol values for fake link
65 uint8_t * AddSymEntry(register uint8_t * buf, SYM * sym, int globflag)
67 // Copy symbol name to buffer (first 8 chars or less)
68 register uint8_t * s = sym->sname;
72 for(i=0; i<8 && *s; i++)
78 register uint16_t w1 = sym->sattr;
79 register uint16_t w = AL_DEFINED | tdb_tab[w1 & TDB];
83 // Extended symbol - Check to see if symbol is larger than 8 characters
84 // and write an extra 14 characters where the next symbol would be.
85 // Modify the flag word for this
88 //printf("%s '%i' - will write extended symbol\n", sym->sname,s[0]);
89 uint8_t *buf2 = buf + 6;
91 for(i=8; i<8+14 && *s; i++)
104 // Construct and deposit flag word
106 // o all symbols are AL_DEFINED
107 // o install T/D/B/A base
108 // o install 'equated'
109 // o commons (COMMON) are AL_EXTERN, but not BSS
110 // o exports (DEFINED) are AL_GLOBAL
111 // o imports (~DEFINED) are AL_EXTERN
113 if (w1 & EQUATED) // Equated
118 w |= AL_EXTERN | AL_GLOBAL; // Common symbol
119 w &= ~AL_BSS; // They're not BSS in Alcyon object files
121 else if (w1 & DEFINED)
123 if (globflag) // Export the symbol
127 w |= AL_EXTERN; // Imported symbol
131 register uint32_t z = (uint32_t)sym->svalue;
133 if (prg_flag) // Relocate value in .PRG segment
138 z += sect[TEXT].sloc;
141 z += sect[DATA].sloc;
144 SETBE32(buf, 0, z); // Deposit symbol value
155 // Add an entry to the BSD symbol table
157 uint8_t * AddBSDSymEntry(uint8_t * buf, SYM * sym, int globflag)
159 chptr = buf; // Point to buffer for depositing longs
160 D_long(strindx); // Deposit the symbol string index
162 uint16_t w1 = sym->sattr; // Obtain symbol attributes
163 uint32_t z = 0; // Initialize resulting symbol flags
167 z = 0x02000000; // Set equated flag
173 case TEXT: z = 0x04000000; break; // Set TEXT segment flag
174 case DATA: z = 0x06000000; break; // Set DATA segment flag
175 case BSS : z = 0x08000000; break; // Set BSS segment flag
180 z |= 0x01000000; // Set global flag if requested
182 D_long(z); // Deposit symbol attribute
183 z = sym->svalue; // Obtain symbol value
185 if (w1 & (DATA | BSS))
186 z += sect[TEXT].sloc; // If DATA or BSS add TEXT segment size
189 z += sect[DATA].sloc; // If BSS add DATA segment size
191 D_long(z); // Deposit symbol value
192 strcpy(strtable + strindx, sym->sname);
193 strindx += strlen(sym->sname) + 1; // Incr string index incl null terminate
194 buf += 12; // Increment buffer to next record
195 symsize += 12; // Increment symbol table size
202 // Add entry to ELF symbol table; if `globflag' is 1, make the symbol global
204 uint8_t * AddELFSymEntry(uint8_t * buf, SYM * sym, int globflag)
208 D_long(strindx); // st_name
209 D_long(sym->svalue); // st_value
210 D_long(0); // st_size
213 register WORD w1 = sym->sattr;
217 //w |= AL_EXTERN | AL_GLOBAL; // common symbol
218 //w &= ~AL_BSS; // they're not BSS in Alcyon object files
220 else if (w1 & DEFINED)
222 if (globflag) // Export the symbol
223 st_info |= 16; //STB_GLOBAL (1<<4)
225 else if (w1 & (GLOBAL | REFERENCED))
229 D_byte(0); // st_other
231 uint16_t st_shndx = 0xFFF1; // Assume absolute (equated) number
234 st_shndx = elfHdrNum[ES_TEXT];
236 st_shndx = elfHdrNum[ES_DATA];
238 st_shndx = elfHdrNum[ES_BSS];
240 st_shndx = 0; // Global, not absolute
244 strcpy(strtable + strindx, sym->sname);
245 strindx += strlen(sym->sname) + 1; // Incr string index incl null terminate
246 symsize += 0x10; // Increment symbol table size
253 // Helper function for ELF output
255 int DepositELFSectionHeader(uint8_t * ptr, uint32_t name, uint32_t type, uint32_t flags, uint32_t addr, uint32_t offset, uint32_t size, uint32_t link, uint32_t info, uint32_t addralign, uint32_t entsize)
274 // Deposit an entry in the Section Header string table
276 uint32_t DepositELFSHSTEntry(uint8_t ** pTable, const uint8_t * s)
279 printf("DepositELFSHSTEntry: s = \"%s\"\n", s);
281 uint32_t strSize = strlen(s);
283 *pTable += strSize + 1;
289 // Deposit a symbol table entry in the ELF Symbol Table
291 uint32_t DepositELFSymbol(uint8_t * ptr, uint32_t name, uint32_t addr, uint32_t size, uint8_t info, uint8_t other, uint16_t shndx)
306 // Write an object file to the passed in file descriptor
307 // N.B.: Return value is ignored...
309 int WriteObject(int fd)
311 LONG t; // Scratch long
312 LONG tds; // TEXT & DATA segment size
313 int i; // Temporary int
314 CHUNK * cp; // Chunk (for gather)
315 uint8_t * buf; // Scratch area
316 uint8_t * p; // Temporary ptr
317 LONG trsize, drsize; // Size of relocations
318 long unused; // For supressing 'write' warnings
322 printf("TEXT segment: %d bytes\n", sect[TEXT].sloc);
323 printf("DATA segment: %d bytes\n", sect[DATA].sloc);
324 printf("BSS segment: %d bytes\n", sect[BSS].sloc);
327 // Write requested object file...
328 if ((obj_format == BSD) || ((obj_format == ALCYON) && (prg_flag == 0)))
330 // Force BSD format (if it was ALCYON format)
335 printf("Total : %d bytes\n", sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc);
338 sy_assign(NULL, NULL); // Assign index numbers to the symbols
339 tds = sect[TEXT].sloc + sect[DATA].sloc; // Get size of TEXT and DATA segment
340 buf = malloc(0x800000); // Allocate 8MB object file image memory
344 error("cannot allocate object file memory (in BSD mode)");
348 memset(buf, 0, 0x800000); // Clear allocated memory
349 objImage = buf; // Set global object image pointer
350 strtable = malloc(0x200000); // Allocate 2MB string table buffer
352 if (strtable == NULL)
354 error("cannot allocate string table memory (in BSD mode)");
358 memset(strtable, 0, 0x200000); // Clear allocated memory
360 // Build object file header
361 chptr = buf; // Base of header (for D_foo macros)
362 D_long(0x00000107); // Magic number
363 D_long(sect[TEXT].sloc); // TEXT size
364 D_long(sect[DATA].sloc); // DATA size
365 D_long(sect[BSS].sloc); // BSS size
366 D_long(0x00000000); // Symbol size
367 D_long(0x00000000); // First entry (0L)
368 D_long(0x00000000); // TEXT relocation size
369 D_long(0x00000000); // DATA relocation size
371 // Construct TEXT and DATA segments (without relocation changes)
372 p = buf + BSDHDRSIZE;
374 for(i=TEXT; i<=DATA; i++)
376 for(cp=sect[i].sfcode; cp!=NULL; cp=cp->chnext)
378 memcpy(p, cp->chptr, cp->ch_size);
383 // Do relocation tables (and make changes to segment data)
384 p = buf + BSDHDRSIZE + tds; // Move obj image ptr to reloc info
385 trsize = MarkBSDImage(p, tds, sect[TEXT].sloc, TEXT);// Do TEXT relocation table
386 chptr = buf + 0x18; // Point to relocation hdr entry
387 D_long(trsize); // Write the relocation table size
389 // Move obj image ptr to reloc info
390 p = buf + BSDHDRSIZE + tds + trsize;
391 drsize = MarkBSDImage(p, tds, sect[TEXT].sloc, DATA);// Do DATA relocation table
392 chptr = buf + 0x1C; // Point to relocation hdr entry
393 D_long(drsize); // Write the relocation table size
395 // Point to start of symbol table
396 p = buf + BSDHDRSIZE + tds + trsize + drsize;
397 sy_assign(p, AddBSDSymEntry); // Build symbol and string tables
398 chptr = buf + 0x10; // Point to sym table size hdr entry
399 D_long(symsize); // Write the symbol table size
401 // Point to string table
402 p = buf + BSDHDRSIZE + tds + trsize + drsize + symsize;
403 memcpy(p, strtable, strindx); // Copy string table to object image
404 chptr = p; // Point to string table size long
405 D_long(strindx); // Write string table size
407 // Write the BSD object file from the object image buffer
408 unused = write(fd, buf, BSDHDRSIZE + tds + trsize + drsize + symsize + strindx + 4);
412 printf("TextRel size: %d bytes\n", trsize);
413 printf("DataRel size: %d bytes\n", drsize);
418 free(strtable); // Free allocated memory
419 free(buf); // Free allocated memory
422 else if (obj_format == ALCYON)
427 printf("TOS header : 28 bytes\n");
429 printf("Total : %d bytes\n", sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc + (prg_flag ? 28 : 0));
432 // Assign index numbers to the symbols, get # of symbols (we assume
433 // that all symbols can potentially be extended, hence the x28)
434 uint32_t symbolMaxSize = sy_assign(NULL, NULL) * 28;
436 // Alloc memory for header + text + data, symbol and relocation
437 // information construction.
438 tds = sect[TEXT].sloc + sect[DATA].sloc;
439 buf = malloc(HDRSIZE + tds + symbolMaxSize);
441 // Build object file header just before the text+data image
442 chptr = buf; // -> base of header
443 D_word(0x601A); // 00 - magic number
444 D_long(sect[TEXT].sloc); // 02 - TEXT size
445 D_long(sect[DATA].sloc); // 06 - DATA size
446 D_long(sect[BSS].sloc); // 0A - BSS size
447 D_long(0); // 0E - symbol table size (filled later)
448 D_long(0); // 12 - stack size (unused)
449 D_long(PRGFLAGS); // 16 - PRGFLAGS
450 D_word(0); // 1A - relocation information exists
452 // Construct text and data segments; fixup relocatable longs in .PRG
453 // mode; finally write the header + text + data
456 for(i=TEXT; i<=DATA; i++)
458 for(cp=sect[i].sfcode; cp!=NULL; cp=cp->chnext)
460 memcpy(p, cp->chptr, cp->ch_size);
465 // Do a first pass on the Alcyon image, if in PRG mode
467 MarkImage(buf + HDRSIZE, tds, sect[TEXT].sloc, 0);
469 // Construct symbol table and update the header entry, if necessary
472 // sy_assign with AddSymEntry updates symsize (stays 0 otherwise)
473 sy_assign(buf + HDRSIZE + tds, AddSymEntry);
474 chptr = buf + 0x0E; // Point to symbol table size entry
478 // Write out the header + text & data + symbol table (if any)
479 unused = write(fd, buf, HDRSIZE + tds + symsize);
481 // Construct and write relocation information; the size of it changes if
482 // we're writing a RELMODed executable. N.B.: Destroys buffer!
483 tds = MarkImage(buf, tds, sect[TEXT].sloc, 1);
484 unused = write(fd, buf, tds);
486 else if (obj_format == ELF)
488 // Allocate 6MB object file image memory
489 buf = malloc(0x600000);
493 error("cannot allocate object file memory (in ELF mode)");
497 memset(buf, 0, 0x600000);
498 objImage = buf; // Set global object image pointer
499 strtable = malloc(0x200000); // Allocate 2MB string table buffer
501 if (strtable == NULL)
503 error("cannot allocate string table memory (in ELF mode)");
507 memset(strtable, 0, 0x200000);
509 // This is pretty much a first pass at this shite, so there's room for
511 uint8_t headers[4 * 10 * 10]; // (DWORD * 10) = 1 hdr, 10 entries
513 uint8_t shstrtab[128]; // The section header string table proper
514 uint32_t shstTab[9]; // Index into shstrtab for strings
515 uint8_t * shstPtr = shstrtab; // Temp pointer
516 uint32_t shstSize = 0;
517 int numEntries = 4; // There are always at *least* 4 sections
518 int shstIndex = 1; // The section where the shstrtab lives
519 int elfSize = 0; // Size of the ELF object
520 // Clear the header numbers
521 memset(elfHdrNum, 0, 9 * sizeof(int));
524 // First step is to see what sections need to be made; we also
525 // construct the section header string table here at the same time.
527 shstTab[ES_NULL] = shstSize;
528 shstSize += DepositELFSHSTEntry(&shstPtr, "");
529 shstTab[ES_SHSTRTAB] = shstSize;
530 shstSize += DepositELFSHSTEntry(&shstPtr, ".shstrtab");
531 shstTab[ES_SYMTAB] = shstSize;
532 shstSize += DepositELFSHSTEntry(&shstPtr, ".symtab");
533 shstTab[ES_STRTAB] = shstSize;
534 shstSize += DepositELFSHSTEntry(&shstPtr, ".strtab");
536 if (sect[TEXT].sloc > 0)
538 elfHdrNum[ES_TEXT] = shstIndex;
539 shstTab[ES_TEXT] = shstSize;
540 shstSize += DepositELFSHSTEntry(&shstPtr, ".text");
545 if (sect[DATA].sloc > 0)
547 elfHdrNum[ES_DATA] = shstIndex;
548 shstTab[ES_DATA] = shstSize;
549 shstSize += DepositELFSHSTEntry(&shstPtr, ".data");
554 if (sect[BSS].sloc > 0)
556 elfHdrNum[ES_BSS] = shstIndex;
557 shstTab[ES_BSS] = shstSize;
558 shstSize += DepositELFSHSTEntry(&shstPtr, ".bss");
563 if (sect[TEXT].relocs > 0)
565 elfHdrNum[ES_RELATEXT] = shstIndex;
566 shstTab[ES_RELATEXT] = shstSize;
567 shstSize += DepositELFSHSTEntry(&shstPtr, ".relaTEXT");
572 if (sect[DATA].relocs > 0)
574 elfHdrNum[ES_RELADATA] = shstIndex;
575 shstTab[ES_RELADATA] = shstSize;
576 shstSize += DepositELFSHSTEntry(&shstPtr, ".relaDATA");
581 elfHdrNum[ES_SHSTRTAB] = shstIndex + 0;
582 elfHdrNum[ES_SYMTAB] = shstIndex + 1;
583 elfHdrNum[ES_STRTAB] = shstIndex + 2;
586 printf("ELF shstrtab size: %i bytes. Entries:\n", shstSize);
587 for(int j=0; j<i; j++)
588 printf("\"%s\"\n", shstrtab + shstTab[j]);
591 // Construct ELF header
592 // If you want to make any sense out of this you'd better take a look
593 // at Executable and Linkable Format on Wikipedia.
595 D_long(0x7F454C46); // 00 - "<7F>ELF" Magic Number
596 D_byte(0x01); // 04 - 32 vs 64 (1 = 32, 2 = 64)
597 D_byte(0x02); // 05 - Endianness (1 = LE, 2 = BE)
598 D_byte(0x01); // 06 - Original version of ELF (set to 1)
599 D_byte(0x00); // 07 - Target OS ABI (0 = System V)
600 D_byte(0x00); // 08 - ABI Extra (unneeded)
601 D_byte(0x00); // 09 - Pad bytes
604 D_word(0x01); // 10 - ELF Type (1 = relocatable)
605 D_word(0x04); // 12 - Architecture (EM_68K = 4, Motorola M68K family)
606 D_long(0x01); // 14 - Version (1 = original ELF)
607 D_long(0x00); // 18 - Entry point virtual address (unneeded)
608 D_long(0x00); // 1C - Program header table offset (unneeded)
609 D_long(0x00); // 20 - Section header table offset (to be determined)
613 // Specifically for 68000 CPU
614 D_long(0x01000000) // 24 - Processor-specific flags - EF_M68K_M68000
618 // CPUs other than 68000 (68020...)
619 D_long(0); // 24 - Processor-specific flags (ISA dependent)
622 D_word(0x0034); // 28 - ELF header size in bytes
623 D_word(0); // 2A - Program header table entry size
624 D_word(0); // 2C - Program header table entry count
625 D_word(0x0028); // 2E - Section header entry size - 40 bytes for ELF32
626 D_word(numEntries); // 30 - Section header table entry count
627 D_word(shstIndex); // 32 - Section header string table index
631 // Deposit section header 0 (NULL)
632 headerSize += DepositELFSectionHeader(headers + headerSize, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
634 int textLoc = elfSize;
636 // Construct TEXT section, if any
637 if (sect[TEXT].sloc > 0)
639 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_TEXT], 1, 6, 0, elfSize, sect[TEXT].sloc, 0, 0, largestAlign[0], 0);
641 for(CHUNK * cp=sect[TEXT].sfcode; cp!=NULL; cp=cp->chnext)
643 memcpy(buf + elfSize, cp->chptr, cp->ch_size);
644 elfSize += cp->ch_size;
647 // Pad for next section (LONG boundary)
648 elfSize = (elfSize + 3) & ~3;
651 int dataLoc = elfSize;
653 // Construct DATA section, if any
654 if (sect[DATA].sloc > 0)
656 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_DATA], 1, 3, 0, elfSize, sect[DATA].sloc, 0, 0, largestAlign[1], 0);
658 for(CHUNK * cp=sect[DATA].sfcode; cp!=NULL; cp=cp->chnext)
660 memcpy(buf + elfSize, cp->chptr, cp->ch_size);
661 elfSize += cp->ch_size;
664 // Pad for next section (LONG boundary)
665 elfSize = (elfSize + 3) & ~3;
668 // Construct BSS section, if any
669 if (sect[BSS].sloc > 0)
671 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_BSS], 8, 3, 0, elfSize, sect[BSS].sloc, 0, 0, largestAlign[2], 0);
674 int textrelLoc = headerSize;
676 // Add headers for relocated sections, if any...
677 if (sect[TEXT].relocs > 0)
678 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_RELATEXT], 4, 0x00, 0, 0, 0, elfHdrNum[ES_SYMTAB], elfHdrNum[ES_TEXT], 4, 0x0C);
680 int datarelLoc = headerSize;
682 if (sect[DATA].relocs > 0)
683 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_RELADATA], 4, 0x40, 0, 0, 0, elfHdrNum[ES_SYMTAB], elfHdrNum[ES_DATA], 4, 0x0C);
686 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_SHSTRTAB], 3, 0, 0, elfSize, shstSize, 0, 0, 1, 0);
687 memcpy(buf + elfSize, shstrtab, shstSize);
689 // Pad for next section (LONG boundary)
690 elfSize = (elfSize + 3) & ~3;
692 // Add section headers
693 int headerLoc = elfSize;
694 chptr = buf + 0x20; // Set section header offset in ELF header
696 elfSize += (4 * 10) * numEntries;
698 // Add symbol table & string table
699 int symtabLoc = elfSize;
700 strindx = 0; // Make sure we start at the beginning...
701 elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 0, 0, 0);
706 if (sect[TEXT].sloc > 0)
708 elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_TEXT]);
712 if (sect[DATA].sloc > 0)
714 elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_DATA]);
718 if (sect[BSS].sloc > 0)
720 elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_BSS]);
724 int numSymbols = sy_assign_ELF(buf + elfSize, AddELFSymEntry);
725 elfSize += numSymbols * 0x10;
728 int strtabLoc = elfSize;
729 memcpy(buf + elfSize, strtable, strindx);
731 // Pad for next section (LONG boundary)
732 elfSize = (elfSize + 3) & ~3;
734 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_SYMTAB], 2, 0, 0, symtabLoc, (numSymbols + extraSyms) * 0x10, shstIndex + 2, firstglobal + extraSyms, 4, 0x10);
735 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_STRTAB], 3, 0, 0, strtabLoc, strindx, 0, 0, 1, 0);
737 // Add relocation tables, if any (no need to align after these, they're
738 // already on DWORD boundaries)
739 if (sect[TEXT].relocs > 0)
741 uint32_t textrelSize = CreateELFRelocationRecord(buf + elfSize, buf + textLoc, TEXT);
742 // Deposit offset & size, now that we know them
743 chptr = headers + textrelLoc + 0x10;
746 elfSize += textrelSize;
749 if (sect[DATA].relocs > 0)
751 uint32_t datarelSize = CreateELFRelocationRecord(buf + elfSize, buf + dataLoc, DATA);
752 // Deposit offset & size, now that we know them
753 chptr = headers + datarelLoc + 0x10;
756 elfSize += datarelSize;
759 // Copy headers into the object
760 memcpy(buf + headerLoc, headers, headerSize);
762 // Finally, write out the object
763 unused = write(fd, buf, elfSize);
765 // Free allocated memory
772 else if (obj_format == XEX)
774 // Just write the object file