2 // RMAC - Reboot's Macro Assembler for the Atari Jaguar Console System
3 // OBJECT.C - Writing Object Files
4 // Copyright (C) 199x Landon Dyer, 2011 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
16 LONG symsize = 0; // Size of BSD symbol table
17 LONG strindx = 0x00000004; // BSD string table index
18 char * strtable; // Pointer to the symbol string table
19 char * objimage; // Global object image pointer
23 // Add an entry to the BSD symbol table
25 char * constr_bsdsymtab(char * buf, SYM * sym, int globflag)
27 LONG z; // Scratch long
28 WORD w1; // Scratch word
29 int w2; // Scratch long
31 chptr = buf; // Point to buffer for deposit longs
32 D_long(strindx); // Deposit the symbol string index
34 w1 = sym->sattr; // Obtain symbol attribute
36 z = 0; // Initialise resulting symbol flags
40 z = 0x02000000; // Set equated flag
46 case TEXT: z = 0x04000000; break; // Set TEXT segment flag
47 case DATA: z = 0x06000000; break; // Set DATA segment flag
48 case BSS : z = 0x08000000; break; // Set BSS segment flag
53 z |= 0x01000000; // Set global flag if requested
55 D_long(z); // Deposit symbol attribute
57 z = sym->svalue; // Obtain symbol value
58 w1 &= DATA | BSS; // Determine DATA or BSS flag
61 z += sect[TEXT].sloc; // If DATA or BSS add TEXT segment size
64 z += sect[DATA].sloc; // If BSS add DATA segment size
66 D_long(z); // Deposit symbol value
68 strcpy(strtable + strindx, sym->sname);
70 strindx += strlen(sym->sname) + 1; // Incr string index incl null terminate
71 buf += 12; // Increment buffer to next record
72 symsize += 12; // Increment symbol table size
79 // Generate object file
83 LONG t; // Scratch long
84 LONG tds; // TEXT & DATA segment size
85 int i; // Temporary int
86 CHUNK * cp; // Chunk (for gather)
87 char * buf; // Scratch area
88 char * p; // Temporary ptr
89 LONG ssize; // Size of symbols
90 LONG trsize, drsize; // Size of relocations
91 long unused; // For supressing 'write' warnings
93 // Write requested object file...
97 ssize = ((LONG)sy_assign(NULL, NULL)); // Assign index numbers to the symbols
98 tds = sect[TEXT].sloc + sect[DATA].sloc; // Get size of TEXT and DATA segment
99 buf = malloc(0x600000); // Allocate 6mb object file image memory
103 error("cannot allocate object file memory (in BSD mode)");
107 memset(buf, 0, 0x600000); // Reset allocated memory
108 objimage = buf; // Set global object image pointer
109 strtable = malloc(0x200000); // Allocate 2mb scratch buffer
111 if (strtable == NULL)
113 error("cannot allocate string table memory (in BSD mode)");
117 memset(strtable, 0, 0x200000); // Reset allocated memory
119 // Build object file header
120 chptr = buf; // Base of header
121 D_long(0x00000107); // Magic number
122 D_long(sect[TEXT].sloc); // TEXT size
123 D_long(sect[DATA].sloc); // DATA size
124 D_long(sect[BSS].sloc); // BSS size
125 D_long(0x00000000); // Symbol size
126 D_long(0x00000000); // First entry (0L)
127 D_long(0x00000000); // TEXT relocation size
128 D_long(0x00000000); // BSD relocation size
130 // Construct TEXT and DATA segments (without relocation changes)
131 p = buf + BSDHDRSIZE;
133 for(i=TEXT; i<=DATA; ++i)
135 for(cp=sect[i].sfcode; cp!=NULL; cp=cp->chnext)
137 copy(p, cp->chptr, cp->ch_size);
142 // Do relocation tables (and make changes to segment data)
143 p = buf + (BSDHDRSIZE + tds); // Move obj image ptr to reloc info
144 trsize = bsdmarkimg(p, tds, sect[TEXT].sloc, TEXT);// Do TEXT relocation table
145 chptr = buf + 24; // Point to relocation hdr entry
146 D_long(trsize); // Write the relocation table size
147 p = buf + (BSDHDRSIZE + tds + trsize); // Move obj image ptr to reloc info
148 drsize = bsdmarkimg(p, tds, sect[TEXT].sloc, DATA);// Do DATA relocation table
149 chptr = buf + 28; // Point to relocation hdr entry
150 D_long(drsize); // Write the relocation table size
152 p = buf + (BSDHDRSIZE + tds + trsize + drsize);// Point to start of symbol table
153 sy_assign(p, constr_bsdsymtab); // Build symbol and string tables
154 chptr = buf + 16; // Point to sym table size hdr entry
155 D_long(symsize); // Write the symbol table size
157 // Point to string table
158 p = buf + (BSDHDRSIZE + tds + trsize + drsize + symsize);
160 memcpy(p, strtable, strindx); // Copy string table to object image
163 free(strtable); // Free allocated memory
165 chptr = p; // Point to string table size long
166 D_long(strindx); // Write string table size
168 // Write the BSD object file from the object image buffer
169 unused = write(fd, buf, BSDHDRSIZE + tds + trsize + drsize + symsize + strindx + 4);
172 free(buf); // Free allocated memory