]> Shamusworld >> Repos - rmac/blob - object.c
522a4d918ce6bacc5d5b0bd0c3805a1867a3dd2c
[rmac] / object.c
1 //
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
7 //
8
9 #include "object.h"
10 #include "6502.h"
11 #include "direct.h"
12 #include "error.h"
13 #include "mark.h"
14 #include "riscasm.h"
15 #include "sect.h"
16 #include "symbol.h"
17
18 //#define DEBUG_ELF
19
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 };
25 uint32_t extraSyms;
26
27 static uint16_t tdb_tab[] = {
28         0,                              // absolute
29         AL_TEXT,                // TEXT segment based
30         AL_DATA, 0,             // DATA segment based
31         AL_BSS                  // BSS segment based
32 };
33
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
43                                                 from standard RAM.
44 -                               3               Currently unused
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
57 */
58
59
60 //
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
64 //
65 uint8_t * AddSymEntry(register uint8_t * buf, SYM * sym, int globflag)
66 {
67         // Copy symbol name to buffer (first 8 chars or less)
68         register uint8_t * s = sym->sname;
69         register int i;
70
71         for(i=0; i<8 && *s; i++)
72                 *buf++ = *s++;
73
74         while (i++ < 8)
75                 *buf++ = '\0';
76
77         //
78         // Construct and deposit flag word
79         //
80         // o  all symbols are AL_DEFINED
81         // o  install T/D/B/A base
82         //   o  install 'equated'
83         //   o  commons (COMMON) are AL_EXTERN, but not BSS
84         // o  exports (DEFINED) are AL_GLOBAL
85         // o  imports (~DEFINED) are AL_EXTERN
86         //
87         register uint16_t w1 = sym->sattr;
88         register uint16_t w = AL_DEFINED | tdb_tab[w1 & TDB];
89
90         if (w1 & EQUATED)               // Equated
91                 w |= AL_EQUATED;
92
93         if (w1 & COMMON)
94         {
95                 w |= AL_EXTERN | AL_GLOBAL;     // Common symbol
96                 w &= ~AL_BSS;           // They're not BSS in Alcyon object files
97         }
98         else if (w1 & DEFINED)
99         {
100                 if (globflag)           // Export the symbol
101                         w |= AL_GLOBAL;
102         }
103         else
104                 w |= AL_EXTERN;         // Imported symbol
105
106         SETBE16(buf, 0, w);
107         buf += 2;
108         register uint32_t z = sym->svalue;
109
110         if (prg_flag)                   // Relocate value in .PRG segment
111         {
112                 w1 &= DATA | BSS;
113
114                 if (w1)
115                         z += sect[TEXT].sloc;
116
117                 if (w1 & BSS)
118                         z += sect[DATA].sloc;
119         }
120
121         SETBE32(buf, 0, z);             // Deposit symbol value
122         buf += 4;
123
124         return buf;
125 }
126
127
128 //
129 // Add an entry to the BSD symbol table
130 //
131 uint8_t * AddBSDSymEntry(uint8_t * buf, SYM * sym, int globflag)
132 {
133         chptr = buf;                                            // Point to buffer for depositing longs
134         D_long(strindx);                                        // Deposit the symbol string index
135
136         uint16_t w1 = sym->sattr;                       // Obtain symbol attributes
137         uint32_t z = 0;                                         // Initialize resulting symbol flags
138
139         if (w1 & EQUATED)
140         {
141                 z = 0x02000000;                                 // Set equated flag
142         }
143         else
144         {
145                 switch (w1 & TDB)
146                 {
147                 case TEXT: z = 0x04000000; break;       // Set TEXT segment flag
148                 case DATA: z = 0x06000000; break;       // Set DATA segment flag
149                 case BSS : z = 0x08000000; break;       // Set BSS segment flag
150                 }
151         }
152
153         if (globflag)
154                 z |= 0x01000000;                                // Set global flag if requested
155
156         D_long(z);                                                      // Deposit symbol attribute
157         z = sym->svalue;                                        // Obtain symbol value
158
159         if (w1 & (DATA | BSS))
160                 z += sect[TEXT].sloc;                   // If DATA or BSS add TEXT segment size
161
162         if (w1 & BSS)
163                 z += sect[DATA].sloc;                   // If BSS add DATA segment size
164
165         D_long(z);                                                      // Deposit symbol value
166         strcpy(strtable + strindx, sym->sname);
167         strindx += strlen(sym->sname) + 1;      // Incr string index incl null terminate
168         buf += 12;                                                      // Increment buffer to next record
169         symsize += 12;                                          // Increment symbol table size
170
171         return buf;
172 }
173
174
175 //
176 // Add entry to ELF symbol table; if `globflag' is 1, make the symbol global
177 //
178 uint8_t * AddELFSymEntry(uint8_t * buf, SYM * sym, int globflag)
179 {
180         chptr = buf;
181         D_long(strindx);                // st_name
182         D_long(sym->svalue);    // st_value
183         D_long(0);                              // st_size
184         uint8_t st_info = 0;
185
186         register WORD w1 = sym->sattr;
187
188         if (w1 & COMMON)
189         {
190                 //w |= AL_EXTERN | AL_GLOBAL;   // common symbol
191                 //w &= ~AL_BSS;         // they're not BSS in Alcyon object files
192         }
193         else if (w1 & DEFINED)
194         {
195                 if (globflag)           // Export the symbol
196                         st_info |= 16;   //STB_GLOBAL (1<<4)
197         }
198         else if (w1 & (GLOBAL | REFERENCED))
199                 st_info |= 16;
200
201         D_byte(st_info);
202         D_byte(0);                              // st_other
203
204         uint16_t st_shndx = 0xFFF1;     // Assume absolute (equated) number
205
206         if (w1 & TEXT)
207                 st_shndx = elfHdrNum[ES_TEXT];
208         else if (w1 & DATA)
209                 st_shndx = elfHdrNum[ES_DATA];
210         else if (w1 & BSS)
211                 st_shndx = elfHdrNum[ES_BSS];
212         else if (globflag)
213                 st_shndx = 0;           // Global, not absolute
214
215         D_word(st_shndx);
216
217         strcpy(strtable + strindx, sym->sname);
218         strindx += strlen(sym->sname) + 1;      // Incr string index incl null terminate
219         symsize += 0x10;                                        // Increment symbol table size
220
221         return buf + 0x10;
222 }
223
224
225 //
226 // Helper function for ELF output
227 //
228 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)
229 {
230         chptr = ptr;
231         D_long(name);
232         D_long(type);
233         D_long(flags);
234         D_long(addr);
235         D_long(offset);
236         D_long(size);
237         D_long(link);
238         D_long(info);
239         D_long(addralign);
240         D_long(entsize);
241         return 40;
242 }
243
244
245 //
246 // Deposit an entry in the Section Header string table
247 //
248 uint32_t DepositELFSHSTEntry(uint8_t ** pTable, const uint8_t * s)
249 {
250 #ifdef DEBUG_ELF
251 printf("DepositELFSHSTEntry: s = \"%s\"\n", s);
252 #endif
253         uint32_t strSize = strlen(s);
254         strcpy(*pTable, s);
255         *pTable += strSize + 1;
256         return strSize + 1;
257 }
258
259
260 //
261 // Deposit a symbol table entry in the ELF Symbol Table
262 //
263 uint32_t DepositELFSymbol(uint8_t * ptr, uint32_t name, uint32_t addr, uint32_t size, uint8_t info, uint8_t other, uint16_t shndx)
264 {
265         chptr = ptr;
266         D_long(name);
267         D_long(addr);
268         D_long(size);
269         *chptr++ = info;
270         *chptr++ = other;
271         D_word(shndx);
272         return 16;
273 }
274
275
276 //
277 // Write an object file to the passed in file descriptor
278 // N.B.: Return value is ignored...
279 //
280 int WriteObject(int fd)
281 {
282         LONG t;                                 // Scratch long
283         LONG tds;                               // TEXT & DATA segment size
284         int i;                                  // Temporary int
285         CHUNK * cp;                             // Chunk (for gather)
286         uint8_t * buf;                  // Scratch area
287         uint8_t * p;                    // Temporary ptr
288         LONG ssize;                             // Size of symbols
289         LONG trsize, drsize;    // Size of relocations
290         long unused;                    // For supressing 'write' warnings
291
292         if (verb_flag)
293         {
294                 printf("TEXT segment: %d bytes\n", sect[TEXT].sloc);
295                 printf("DATA segment: %d bytes\n", sect[DATA].sloc);
296                 printf("BSS  segment: %d bytes\n", sect[BSS].sloc);
297         }
298
299         // Write requested object file...
300         if ((obj_format == BSD) || ((obj_format == ALCYON) && (prg_flag == 0)))
301     {
302                 // Force BSD format (if it was ALCYON format)
303                 obj_format = BSD;
304
305                 if (verb_flag)
306                 {
307                         printf("Total       : %d bytes\n", sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc);
308                 }
309
310                 ssize = sy_assign(NULL, NULL);                          // Assign index numbers to the symbols
311                 tds = sect[TEXT].sloc + sect[DATA].sloc;        // Get size of TEXT and DATA segment
312                 buf = malloc(0x600000);                                         // Allocate 6mb object file image memory
313
314                 if (buf == NULL)
315                 {
316                         error("cannot allocate object file memory (in BSD mode)");
317                         return ERROR;
318                 }
319
320                 memset(buf, 0, 0x600000);               // Clear allocated memory
321                 objImage = buf;                                 // Set global object image pointer
322                 strtable = malloc(0x200000);    // Allocate 2MB string table buffer
323
324                 if (strtable == NULL)
325                 {
326                         error("cannot allocate string table memory (in BSD mode)");
327                         return ERROR;
328                 }
329
330                 memset(strtable, 0, 0x200000);  // Clear allocated memory
331
332                 // Build object file header
333                 chptr = buf;                                    // Base of header (for D_foo macros)
334                 D_long(0x00000107);                             // Magic number
335                 D_long(sect[TEXT].sloc);                // TEXT size
336                 D_long(sect[DATA].sloc);                // DATA size
337                 D_long(sect[BSS].sloc);                 // BSS size
338                 D_long(0x00000000);                             // Symbol size
339                 D_long(0x00000000);                             // First entry (0L)
340                 D_long(0x00000000);                             // TEXT relocation size
341                 D_long(0x00000000);                             // DATA relocation size
342
343                 // Construct TEXT and DATA segments (without relocation changes)
344                 p = buf + BSDHDRSIZE;
345
346                 for(i=TEXT; i<=DATA; i++)
347                 {
348                         for(cp=sect[i].sfcode; cp!=NULL; cp=cp->chnext)
349                         {
350                                 memcpy(p, cp->chptr, cp->ch_size);
351                                 p += cp->ch_size;
352                         }
353                 }
354
355                 // Do relocation tables (and make changes to segment data)
356                 p = buf + BSDHDRSIZE + tds;             // Move obj image ptr to reloc info
357                 trsize = MarkBSDImage(p, tds, sect[TEXT].sloc, TEXT);// Do TEXT relocation table
358                 chptr = buf + 0x18;                             // Point to relocation hdr entry
359                 D_long(trsize);                                 // Write the relocation table size
360
361                 // Move obj image ptr to reloc info
362                 p = buf + BSDHDRSIZE + tds + trsize;
363                 drsize = MarkBSDImage(p, tds, sect[TEXT].sloc, DATA);// Do DATA relocation table
364                 chptr = buf + 0x1C;                             // Point to relocation hdr entry
365                 D_long(drsize);                                 // Write the relocation table size
366
367                 // Point to start of symbol table
368                 p = buf + BSDHDRSIZE + tds + trsize + drsize;
369                 sy_assign(p, AddBSDSymEntry);   // Build symbol and string tables
370                 chptr = buf + 0x10;                             // Point to sym table size hdr entry
371                 D_long(symsize);                                // Write the symbol table size
372
373                 // Point to string table
374                 p = buf + BSDHDRSIZE + tds + trsize + drsize + symsize;
375                 memcpy(p, strtable, strindx);   // Copy string table to object image
376                 chptr = p;                                              // Point to string table size long
377                 D_long(strindx);                                // Write string table size
378
379                 // Write the BSD object file from the object image buffer
380                 unused = write(fd, buf, BSDHDRSIZE + tds + trsize + drsize + symsize + strindx + 4);
381
382                 if (verb_flag)
383                 {
384                         printf("TextRel size: %d bytes\n", trsize);
385                         printf("DataRel size: %d bytes\n", drsize);
386                 }
387
388                 if (buf)
389                 {
390                         free(strtable);                         // Free allocated memory
391                         free(buf);                                      // Free allocated memory
392                 }
393         }
394         else if (obj_format == ALCYON)
395         {
396                 if (verb_flag)
397                 {
398                         if (prg_flag)
399                         {
400                                 printf("TOS header  : 28 bytes\n");
401                                 printf("Total       : %d bytes\n", 28 + sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc);
402                         }
403                         else
404                         {
405                                 printf("Total       : %d bytes\n", sect[TEXT].sloc + sect[DATA].sloc + sect[BSS].sloc);
406                         }
407                 }
408
409                 // Compute size of symbol table; assign numbers to the symbols...
410                 ssize = 0;
411
412                 // As we grabbed BSD *and* Alcyon in prg_flag == 0 mode, this is *always*
413                 // false... :-P
414                 if (prg_flag != 1)
415                         ssize = sy_assign(NULL, NULL) * 14;
416
417                 // Alloc memory for header + text + data, symbol and relocation
418                 // information construction.
419                 t = tds = sect[TEXT].sloc + sect[DATA].sloc;
420
421                 if (t < ssize)
422                         t = ssize;
423
424                 // Is there any reason to do this this way???
425                 buf = malloc(t + HDRSIZE);
426                 buf += HDRSIZE;
427
428                 // Build object file header just before the text+data image
429                 chptr = buf - HDRSIZE;          // -> base of header
430                 D_word(0x601A);                         // 00 - magic number
431                 D_long(sect[TEXT].sloc);        // 02 - TEXT size
432                 D_long(sect[DATA].sloc);        // 06 - DATA size
433                 D_long(sect[BSS].sloc);         // 0A - BSS size
434                 D_long(ssize);                          // 0E - symbol table size
435                 D_long(0);                                      // 12 - stack size (unused)
436                 D_long(PRGFLAGS);                       // 16 - PRGFLAGS
437                 D_word(0);                                      // 1A - relocation information exists
438
439                 // Construct text and data segments; fixup relocatable longs in .PRG
440                 // mode; finally write the header + text + data
441                 p = buf;
442
443                 for(i=TEXT; i<=DATA; i++)
444                 {
445                         for(cp=sect[i].sfcode; cp!=NULL; cp=cp->chnext)
446                         {
447                                 memcpy(p, cp->chptr, cp->ch_size);
448                                 p += cp->ch_size;
449                         }
450                 }
451
452                 // Do a first pass on the Alcyon image, if in PRG mode
453                 if (prg_flag)
454                         MarkImage(buf, tds, sect[TEXT].sloc, 0);
455
456                 unused = write(fd, buf - HDRSIZE, tds + HDRSIZE);
457
458                 // Construct and write symbol table
459                 if (prg_flag != 1)
460                 {
461                         sy_assign(buf, AddSymEntry);
462                         unused = write(fd, buf, ssize);
463                 }
464
465                 // Construct and write relocation information; the size of it changes if
466                 // we're writing a RELMODed executable.
467                 tds = MarkImage(buf, tds, sect[TEXT].sloc, 1);
468                 unused = write(fd, buf, tds);
469         }
470         else if (obj_format == ELF)
471         {
472                 // Allocate 6MB object file image memory
473                 buf = malloc(0x600000);
474
475                 if (buf == NULL)
476                 {
477                         error("cannot allocate object file memory (in BSD mode)");
478                         return ERROR;
479                 }
480
481                 memset(buf, 0, 0x600000);
482                 objImage = buf;                                 // Set global object image pointer
483                 strtable = malloc(0x200000);    // Allocate 2MB string table buffer
484
485                 if (strtable == NULL)
486                 {
487                         error("cannot allocate string table memory (in BSD mode)");
488                         return ERROR;
489                 }
490
491                 memset(strtable, 0, 0x200000);
492
493                 // This is pretty much a first pass at this shite, so there's room for
494                 // improvement. :-P
495                 uint8_t headers[4 * 10 * 10];   // (DWORD * 10) = 1 hdr, 10 entries
496                 int headerSize = 0;
497                 uint8_t shstrtab[128];                  // The section header string table proper
498                 uint32_t shstTab[9];                    // Index into shstrtab for strings
499                 uint8_t * shstPtr = shstrtab;   // Temp pointer
500                 uint32_t shstSize = 0;
501                 int numEntries = 4;                             // There are always at *least* 4 sections
502                 int shstIndex = 1;                              // The section where the shstrtab lives
503                 int elfSize = 0;                                // Size of the ELF object
504                 // Clear the header numbers
505                 memset(elfHdrNum, 0, 9 * sizeof(int));
506
507                 //
508                 // First step is to see what sections need to be made; we also
509                 // construct the section header string table here at the same time.
510                 //
511                 shstTab[ES_NULL] = shstSize;
512                 shstSize += DepositELFSHSTEntry(&shstPtr, "");
513                 shstTab[ES_SHSTRTAB] = shstSize;
514                 shstSize += DepositELFSHSTEntry(&shstPtr, ".shstrtab");
515                 shstTab[ES_SYMTAB] = shstSize;
516                 shstSize += DepositELFSHSTEntry(&shstPtr, ".symtab");
517                 shstTab[ES_STRTAB] = shstSize;
518                 shstSize += DepositELFSHSTEntry(&shstPtr, ".strtab");
519
520                 if (sect[TEXT].sloc > 0)
521                 {
522                         elfHdrNum[ES_TEXT] = shstIndex;
523                         shstTab[ES_TEXT] = shstSize;
524                         shstSize += DepositELFSHSTEntry(&shstPtr, ".text");
525                         shstIndex++;
526                         numEntries++;
527                 }
528
529                 if (sect[DATA].sloc > 0)
530                 {
531                         elfHdrNum[ES_DATA] = shstIndex;
532                         shstTab[ES_DATA] = shstSize;
533                         shstSize += DepositELFSHSTEntry(&shstPtr, ".data");
534                         shstIndex++;
535                         numEntries++;
536                 }
537
538                 if (sect[BSS].sloc > 0)
539                 {
540                         elfHdrNum[ES_BSS] = shstIndex;
541                         shstTab[ES_BSS] = shstSize;
542                         shstSize += DepositELFSHSTEntry(&shstPtr, ".bss");
543                         shstIndex++;
544                         numEntries++;
545                 }
546
547                 if (sect[TEXT].relocs > 0)
548                 {
549                         elfHdrNum[ES_RELATEXT] = shstIndex;
550                         shstTab[ES_RELATEXT] = shstSize;
551                         shstSize += DepositELFSHSTEntry(&shstPtr, ".relaTEXT");
552                         shstIndex++;
553                         numEntries++;
554                 }
555
556                 if (sect[DATA].relocs > 0)
557                 {
558                         elfHdrNum[ES_RELADATA] = shstIndex;
559                         shstTab[ES_RELADATA] = shstSize;
560                         shstSize += DepositELFSHSTEntry(&shstPtr, ".relaDATA");
561                         shstIndex++;
562                         numEntries++;
563                 }
564
565                 elfHdrNum[ES_SHSTRTAB] = shstIndex + 0;
566                 elfHdrNum[ES_SYMTAB]   = shstIndex + 1;
567                 elfHdrNum[ES_STRTAB]   = shstIndex + 2;
568
569 #ifdef DEBUG_ELF
570 printf("ELF shstrtab size: %i bytes. Entries:\n", shstSize);
571 for(int j=0; j<i; j++)
572         printf("\"%s\"\n", shstrtab + shstTab[j]);
573 #endif
574
575                 // Construct ELF header
576                 // If you want to make any sense out of this you'd better take a look
577                 // at Executable and Linkable Format on Wikipedia.
578                 chptr = buf;
579                 D_long(0x7F454C46); // 00 - "<7F>ELF" Magic Number
580                 D_byte(0x01); // 04 - 32 vs 64 (1 = 32, 2 = 64)
581                 D_byte(0x02); // 05 - Endianness (1 = LE, 2 = BE)
582                 D_byte(0x01); // 06 - Original version of ELF (set to 1)
583                 D_byte(0x00); // 07 - Target OS ABI (0 = System V)
584                 D_byte(0x00); // 08 - ABI Extra (unneeded)
585                 D_byte(0x00); // 09 - Pad bytes
586                 D_word(0x00);
587                 D_long(0x00);
588                 D_word(0x01); // 10 - ELF Type (1 = relocatable)
589                 D_word(0x04); // 12 - Architecture (EM_68K = 4, Motorola M68K family)
590                 D_long(0x01); // 14 - Version (1 = original ELF)
591                 D_long(0x00); // 18 - Entry point virtual address (unneeded)
592                 D_long(0x00); // 1C - Program header table offset (unneeded)
593                 D_long(0x00); // 20 - Section header table offset (to be determined)
594
595                 if (0)
596                 {
597                         // Specifically for 68000 CPU
598                         D_long(0x01000000) // 24 - Processor-specific flags - EF_M68K_M68000
599                 }
600                 else
601                 {
602                         // CPUs other than 68000 (68020...)
603                         D_long(0); // 24 - Processor-specific flags (ISA dependent)
604                 }
605
606                 D_word(0x0034); // 28 - ELF header size in bytes
607                 D_word(0); // 2A - Program header table entry size
608                 D_word(0); // 2C - Program header table entry count
609                 D_word(0x0028); // 2E - Section header entry size - 40 bytes for ELF32
610                 D_word(numEntries); // 30 - Section header table entry count
611                 D_word(shstIndex); // 32 - Section header string table index
612
613                 elfSize += 0x34;
614
615                 // Deposit section header 0 (NULL)
616                 headerSize += DepositELFSectionHeader(headers + headerSize, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
617
618                 int textLoc = elfSize;
619
620                 // Construct TEXT section, if any
621                 if (sect[TEXT].sloc > 0)
622                 {
623                         headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_TEXT], 1, 6, 0, elfSize, sect[TEXT].sloc, 0, 0, largestAlign[0], 0);
624
625                         for(CHUNK * cp=sect[TEXT].sfcode; cp!=NULL; cp=cp->chnext)
626                         {
627                                 memcpy(buf + elfSize, cp->chptr, cp->ch_size);
628                                 elfSize += cp->ch_size;
629                         }
630
631                         // Pad for next section (LONG boundary)
632                         elfSize = (elfSize + 3) & ~3;
633                 }
634
635                 int dataLoc = elfSize;
636
637                 // Construct DATA section, if any
638                 if (sect[DATA].sloc > 0)
639                 {
640                         headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_DATA], 1, 3, 0, elfSize, sect[DATA].sloc, 0, 0, largestAlign[1], 0);
641
642                         for(CHUNK * cp=sect[DATA].sfcode; cp!=NULL; cp=cp->chnext)
643                         {
644                                 memcpy(buf + elfSize, cp->chptr, cp->ch_size);
645                                 elfSize += cp->ch_size;
646                         }
647
648                         // Pad for next section (LONG boundary)
649                         elfSize = (elfSize + 3) & ~3;
650                 }
651
652                 // Construct BSS section, if any
653                 if (sect[BSS].sloc > 0)
654                 {
655                         headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_BSS], 8, 3, 0, elfSize, sect[BSS].sloc, 0, 0, largestAlign[2], 0);
656                 }
657
658                 int textrelLoc = headerSize;
659
660                 // Add headers for relocated sections, if any...
661                 if (sect[TEXT].relocs > 0)
662                         headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_RELATEXT], 4, 0x00, 0, 0, 0, elfHdrNum[ES_SYMTAB], elfHdrNum[ES_TEXT], 4, 0x0C);
663
664                 int datarelLoc = headerSize;
665
666                 if (sect[DATA].relocs > 0)
667                         headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_RELADATA], 4, 0x40, 0, 0, 0, elfHdrNum[ES_SYMTAB], elfHdrNum[ES_DATA], 4, 0x0C);
668
669                 // Add shstrtab
670                 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_SHSTRTAB], 3, 0, 0, elfSize, shstSize, 0, 0, 1, 0);
671                 memcpy(buf + elfSize, shstrtab, shstSize);
672                 elfSize += shstSize;
673                 // Pad for next section (LONG boundary)
674                 elfSize = (elfSize + 3) & ~3;
675
676                 // Add section headers
677                 int headerLoc = elfSize;
678                 chptr = buf + 0x20;             // Set section header offset in ELF header
679                 D_long(headerLoc);
680                 elfSize += (4 * 10) * numEntries;
681
682                 // Add symbol table & string table
683                 int symtabLoc = elfSize;
684                 strindx = 0;    // Make sure we start at the beginning...
685                 elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 0, 0, 0);
686                 *strtable = 0;
687                 strindx++;
688                 extraSyms = 1;
689
690                 if (sect[TEXT].sloc > 0)
691                 {
692                         elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_TEXT]);
693                         extraSyms++;
694                 }
695
696                 if (sect[DATA].sloc > 0)
697                 {
698                         elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_DATA]);
699                         extraSyms++;
700                 }
701
702                 if (sect[BSS].sloc > 0)
703                 {
704                         elfSize += DepositELFSymbol(buf + elfSize, 0, 0, 0, 3, 0, elfHdrNum[ES_BSS]);
705                         extraSyms++;
706                 }
707
708                 int numSymbols = sy_assign_ELF(buf + elfSize, AddELFSymEntry);
709                 elfSize += numSymbols * 0x10;
710
711                 // String table
712                 int strtabLoc = elfSize;
713                 memcpy(buf + elfSize, strtable, strindx);
714                 elfSize += strindx;
715                 // Pad for next section (LONG boundary)
716                 elfSize = (elfSize + 3) & ~3;
717
718                 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_SYMTAB], 2, 0, 0, symtabLoc, (numSymbols + extraSyms) * 0x10, shstIndex + 2, firstglobal + extraSyms, 4, 0x10);
719                 headerSize += DepositELFSectionHeader(headers + headerSize, shstTab[ES_STRTAB], 3, 0, 0, strtabLoc, strindx, 0, 0, 1, 0);
720
721                 // Add relocation tables, if any (no need to align after these, they're
722                 // already on DWORD boundaries)
723                 if (sect[TEXT].relocs > 0)
724                 {
725                         uint32_t textrelSize = CreateELFRelocationRecord(buf + elfSize, buf + textLoc, TEXT);
726                         // Deposit offset & size, now that we know them
727                         chptr = headers + textrelLoc + 0x10;
728                         D_long(elfSize);
729                         D_long(textrelSize);
730                         elfSize += textrelSize;
731                 }
732
733                 if (sect[DATA].relocs > 0)
734                 {
735                         uint32_t datarelSize = CreateELFRelocationRecord(buf + elfSize, buf + dataLoc, DATA);
736                         // Deposit offset & size, now that we know them
737                         chptr = headers + datarelLoc + 0x10;
738                         D_long(elfSize);
739                         D_long(datarelSize);
740                         elfSize += datarelSize;
741                 }
742
743                 // Copy headers into the object
744                 memcpy(buf + headerLoc, headers, headerSize);
745
746                 // Finally, write out the object
747                 unused = write(fd, buf, elfSize);
748
749                 // Free allocated memory
750                 if (buf)
751                 {
752                         free(buf);
753                         free(strtable);
754                 }
755         }
756         else if (obj_format == XEX)
757         {
758                 // Just write the object file
759                 m6502obj(fd);
760         }
761
762         return 0;
763 }
764