Initial commit.
[rmac] / object.c
1 ////////////////////////////////////////////////////////////////////////////////////////////////////
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
7
8 #include "object.h"
9 #include "sect.h"
10 #include "symbol.h"
11 #include "mark.h"
12 #include "error.h"
13 #include "risca.h"
14
15 LONG symsize = 0;                                           // Size of BSD symbol table
16 LONG strindx = 0x00000004;                                  // BSD string table index
17 char *strtable;                                             // Pointer to the symbol string table
18 char *objimage;                                             // Global object image pointer
19
20 //
21 // --- Add an entry to the BSD symbol table --------------------------------------------------------
22 //
23
24 char *constr_bsdsymtab(char *buf, SYM *sym, int globflag) {
25    LONG z;                                                  // Scratch long
26    WORD w1;                                                 // Scratch word
27    int w2;                                                  // Scratch long
28    
29    chptr = buf;                                             // Point to buffer for deposit longs
30    D_long(strindx);                                         // Deposit the symbol string index
31
32    w1 = sym->sattr;                                         // Obtain symbol attribute
33    w2 = sym->sattre;
34    z = 0;                                                   // Initialise resulting symbol flags
35    if(w1 & EQUATED) {                                       
36       z = 0x02000000;                                       // Set equated flag
37    } else {
38       switch(w1 & TDB) {
39          case TEXT: z = 0x04000000; break;                  // Set TEXT segment flag
40          case DATA: z = 0x06000000; break;                  // Set DATA segment flag
41          case BSS : z = 0x08000000; break;                  // Set BSS segment flag
42       }
43    }
44    if(globflag) z |= 0x01000000;                            // Set global flag if requested
45    D_long(z);                                               // Deposit symbol attribute
46
47    z = sym->svalue;                                         // Obtain symbol value
48    w1 &= DATA|BSS;                                          // Determine DATA or BSS flag
49    if(w1)                                                   
50       z += sect[TEXT].sloc;                                 // If DATA or BSS add TEXT segment size
51    if(w1 & BSS) 
52       z += sect[DATA].sloc;                                 // If BSS add DATA segment size
53    D_long(z);                                               // Deposit symbol value
54
55    strcpy(strtable + strindx, sym->sname);
56
57    strindx += strlen(sym->sname) + 1;                       // Incr string index incl null terminate
58    buf += 12;                                               // Increment buffer to next record
59    symsize += 12;                                           // Increment symbol table size
60
61    return(buf);
62 }
63
64 //
65 // --- Generate object file ------------------------------------------------------------------------
66 //
67
68 int object(WORD fd) {
69    LONG t;                                                  // Scratch long
70    LONG tds;                                                // TEXT & DATA segment size
71    int i;                                                   // Temporary int
72    CHUNK *cp;                                               // Chunk (for gather)
73    char *buf;                                               // Scratch area
74    char *p;                                                 // Temporary ptr
75    LONG ssize;                                              // Size of symbols
76    LONG trsize, drsize;                                     // Size of relocations
77
78    // Write requested object file...
79    switch(obj_format) {
80       case BSD:
81          ssize = ((LONG)sy_assign(NULL, NULL));             // Assign index numbers to the symbols
82          tds = sect[TEXT].sloc + sect[DATA].sloc;           // Get size of TEXT and DATA segment
83          buf = malloc(0x400000);                            // Allocate 4mb object file image memory
84          if(buf == NULL) {
85             error("cannot allocate object file memory (in BSD mode)");
86             return(ERROR);
87          }
88          memset(buf, 0, 0x400000);                          // Reset allocated memory
89          objimage = buf;                                    // Set global object image pointer
90          strtable = malloc(0x200000);                       // Allocate 2mb scratch buffer 
91          if(strtable == NULL) {
92             error("cannot allocate string table memory (in BSD mode)");
93             return(ERROR);
94          }
95          memset(strtable, 0, 0x200000);                     // Reset allocated memory
96
97          // Build object file header
98          chptr = buf;                                       // Base of header
99          t = 0x00000107;
100          D_long(t);                                         // Magic number
101          t = sect[TEXT].sloc;                               // TEXT size 
102          D_long(t);
103          t = sect[DATA].sloc;                               // DATA size 
104          D_long(t);
105          t = sect[BSS].sloc;                                // BSS size 
106          D_long(t);
107          t = 0x00000000;
108          D_long(t);                                         // Symbol size
109          D_long(t);                                         // First entry (0L)
110          D_long(t);                                         // TEXT relocation size
111          D_long(t);                                         // BSD relocation size
112
113          // Construct TEXT and DATA segments (without relocation changes)
114          p = buf + BSDHDRSIZE;
115          for(i = TEXT; i <= DATA; ++i)
116             for(cp = sect[i].sfcode; cp != NULL; cp = cp->chnext) {
117                copy(p, cp->chptr, cp->ch_size);
118                p += cp->ch_size;
119             }
120
121          // Do relocation tables (and make changes to segment data)
122          p = buf + (BSDHDRSIZE + tds);                      // Move obj image ptr to reloc info
123          trsize = bsdmarkimg(p, tds, sect[TEXT].sloc, TEXT);// Do TEXT relocation table
124          chptr = buf + 24;                                  // Point to relocation hdr entry
125          D_long(trsize);                                    // Write the relocation table size
126          p = buf + (BSDHDRSIZE + tds + trsize);             // Move obj image ptr to reloc info
127          drsize = bsdmarkimg(p, tds, sect[TEXT].sloc, DATA);// Do DATA relocation table
128          chptr = buf + 28;                                  // Point to relocation hdr entry
129          D_long(drsize);                                    // Write the relocation table size
130
131          p = buf + (BSDHDRSIZE + tds + trsize + drsize);    // Point to start of symbol table
132          sy_assign(p, constr_bsdsymtab);                    // Build symbol and string tables
133          chptr = buf + 16;                                  // Point to sym table size hdr entry
134          D_long(symsize);                                   // Write the symbol table size
135
136          // Point to string table
137          p = buf + (BSDHDRSIZE + tds + trsize + drsize + symsize);    
138
139          memcpy(p, strtable, strindx);                      // Copy string table to object image
140          if(buf) free(strtable);                            // Free allocated memory
141          chptr = p;                                         // Point to string table size long
142          D_long(strindx);                                   // Write string table size
143
144          // Write the BSD object file from the object image buffer
145          write(fd, buf, BSDHDRSIZE + tds + trsize + drsize + symsize + strindx + 4);
146
147          if(buf) free(buf);                                 // Free allocated memory
148          break;
149    }
150
151    return(0);
152 }
153
154
155
156
157
158