Change rights 644 to 755
[nasm/github.git] / output / outmac32.c
blob616211eb2876219b8e4aa34f80964b6245c36189
1 /* ----------------------------------------------------------------------- *
2 *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * outmacho.c output routines for the Netwide Assembler to produce
36 * NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
39 /* Most of this file is, like Mach-O itself, based on a.out. For more
40 * guidelines see outaout.c. */
42 #include "compiler.h"
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <inttypes.h>
50 #include "nasm.h"
51 #include "nasmlib.h"
52 #include "saa.h"
53 #include "raa.h"
54 #include "eval.h"
55 #include "output/outform.h"
56 #include "output/outlib.h"
58 #if defined(OF_MACHO32)
60 /* Mach-O in-file header structure sizes */
61 #define MACHO_HEADER_SIZE (28)
62 #define MACHO_SEGCMD_SIZE (56)
63 #define MACHO_SECTCMD_SIZE (68)
64 #define MACHO_SYMCMD_SIZE (24)
65 #define MACHO_NLIST_SIZE (12)
66 #define MACHO_RELINFO_SIZE (8)
68 /* Mach-O file header values */
69 #define MH_MAGIC (0xfeedface)
70 #define CPU_TYPE_I386 (7) /* x86 platform */
71 #define CPU_SUBTYPE_I386_ALL (3) /* all-x86 compatible */
72 #define MH_OBJECT (0x1) /* object file */
74 #define LC_SEGMENT (0x1) /* segment load command */
75 #define LC_SYMTAB (0x2) /* symbol table load command */
77 #define VM_PROT_NONE (0x00)
78 #define VM_PROT_READ (0x01)
79 #define VM_PROT_WRITE (0x02)
80 #define VM_PROT_EXECUTE (0x04)
82 #define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
83 #define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
85 struct section {
86 /* nasm internal data */
87 struct section *next;
88 struct SAA *data;
89 int32_t index;
90 struct reloc *relocs;
91 int align;
93 /* data that goes into the file */
94 char sectname[16]; /* what this section is called */
95 char segname[16]; /* segment this section will be in */
96 uint32_t addr; /* in-memory address (subject to alignment) */
97 uint32_t size; /* in-memory and -file size */
98 uint32_t nreloc; /* relocation entry count */
99 uint32_t flags; /* type and attributes (masked) */
102 #define SECTION_TYPE 0x000000ff /* section type mask */
104 #define S_REGULAR (0x0) /* standard section */
105 #define S_ZEROFILL (0x1) /* zerofill, in-memory only */
107 #define SECTION_ATTRIBUTES_SYS 0x00ffff00 /* system setable attributes */
108 #define S_ATTR_SOME_INSTRUCTIONS 0x00000400 /* section contains some
109 machine instructions */
110 #define S_ATTR_EXT_RELOC 0x00000200 /* section has external
111 relocation entries */
112 #define S_ATTR_LOC_RELOC 0x00000100 /* section has local
113 relocation entries */
116 static struct sectmap {
117 const char *nasmsect;
118 const char *segname;
119 const char *sectname;
120 const int32_t flags;
121 } sectmap[] = {
122 {".text", "__TEXT", "__text", S_REGULAR|S_ATTR_SOME_INSTRUCTIONS},
123 {".data", "__DATA", "__data", S_REGULAR},
124 {".rodata", "__DATA", "__const", S_REGULAR},
125 {".bss", "__DATA", "__bss", S_ZEROFILL},
126 {NULL, NULL, NULL, 0}
129 struct reloc {
130 /* nasm internal data */
131 struct reloc *next;
133 /* data that goes into the file */
134 int32_t addr; /* op's offset in section */
135 unsigned int snum:24, /* contains symbol index if
136 ** ext otherwise in-file
137 ** section number */
138 pcrel:1, /* relative relocation */
139 length:2, /* 0=byte, 1=word, 2=int32_t */
140 ext:1, /* external symbol referenced */
141 type:4; /* reloc type, 0 for us */
144 #define R_ABS 0 /* absolute relocation */
145 #define R_SCATTERED 0x80000000 /* reloc entry is scattered if
146 ** highest bit == 1 */
148 struct symbol {
149 /* nasm internal data */
150 struct symbol *next; /* next symbol in the list */
151 char *name; /* name of this symbol */
152 int32_t initial_snum; /* symbol number used above in
153 reloc */
154 int32_t snum; /* true snum for reloc */
156 /* data that goes into the file */
157 int32_t strx; /* string table index */
158 uint8_t type; /* symbol type */
159 uint8_t sect; /* NO_SECT or section number */
160 int16_t desc; /* for stab debugging, 0 for us */
161 uint32_t value; /* offset of symbol in section */
164 /* symbol type bits */
165 #define N_EXT 0x01 /* global or external symbol */
167 #define N_UNDF 0x0 /* undefined symbol | n_sect == */
168 #define N_ABS 0x2 /* absolute symbol | NO_SECT */
169 #define N_SECT 0xe /* defined symbol, n_sect holds
170 ** section number */
172 #define N_TYPE 0x0e /* type bit mask */
174 #define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
176 /* special section number values */
177 #define NO_SECT 0 /* no section, invalid */
178 #define MAX_SECT 255 /* maximum number of sections */
180 static struct section *sects, **sectstail;
181 static struct symbol *syms, **symstail;
182 static uint32_t nsyms;
184 /* These variables are set by macho_layout_symbols() to organize
185 the symbol table and string table in order the dynamic linker
186 expects. They are then used in macho_write() to put out the
187 symbols and strings in that order.
189 The order of the symbol table is:
190 local symbols
191 defined external symbols (sorted by name)
192 undefined external symbols (sorted by name)
194 The order of the string table is:
195 strings for external symbols
196 strings for local symbols
198 static uint32_t ilocalsym = 0;
199 static uint32_t iextdefsym = 0;
200 static uint32_t iundefsym = 0;
201 static uint32_t nlocalsym;
202 static uint32_t nextdefsym;
203 static uint32_t nundefsym;
204 static struct symbol **extdefsyms = NULL;
205 static struct symbol **undefsyms = NULL;
207 static struct RAA *extsyms;
208 static struct SAA *strs;
209 static uint32_t strslen;
211 /* Global file information. This should be cleaned up into either
212 a structure or as function arguments. */
213 uint32_t head_ncmds = 0;
214 uint32_t head_sizeofcmds = 0;
215 uint32_t seg_filesize = 0;
216 uint32_t seg_vmsize = 0;
217 uint32_t seg_nsects = 0;
218 uint32_t rel_padcnt = 0;
221 #define xstrncpy(xdst, xsrc) \
222 memset(xdst, '\0', sizeof(xdst)); /* zero out whole buffer */ \
223 strncpy(xdst, xsrc, sizeof(xdst)); /* copy over string */ \
224 xdst[sizeof(xdst) - 1] = '\0'; /* proper null-termination */
226 #define alignint32_t(x) \
227 ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
229 static void debug_reloc (struct reloc *);
230 static void debug_section_relocs (struct section *) _unused;
232 static struct section *get_section_by_name(const char *segname,
233 const char *sectname)
235 struct section *s;
237 for (s = sects; s != NULL; s = s->next)
238 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
239 break;
241 return s;
244 static struct section *get_section_by_index(const int32_t index)
246 struct section *s;
248 for (s = sects; s != NULL; s = s->next)
249 if (index == s->index)
250 break;
252 return s;
255 static int32_t get_section_index_by_name(const char *segname,
256 const char *sectname)
258 struct section *s;
260 for (s = sects; s != NULL; s = s->next)
261 if (!strcmp(s->segname, segname) && !strcmp(s->sectname, sectname))
262 return s->index;
264 return -1;
267 static char *get_section_name_by_index(const int32_t index)
269 struct section *s;
271 for (s = sects; s != NULL; s = s->next)
272 if (index == s->index)
273 return s->sectname;
275 return NULL;
278 static uint8_t get_section_fileindex_by_index(const int32_t index)
280 struct section *s;
281 uint8_t i = 1;
283 for (s = sects; s != NULL && i < MAX_SECT; s = s->next, ++i)
284 if (index == s->index)
285 return i;
287 if (i == MAX_SECT)
288 nasm_error(ERR_WARNING,
289 "too many sections (>255) - clipped by fileindex");
291 return NO_SECT;
294 static void macho_init(void)
296 char zero = 0;
298 sects = NULL;
299 sectstail = &sects;
301 syms = NULL;
302 symstail = &syms;
303 nsyms = 0;
304 nlocalsym = 0;
305 nextdefsym = 0;
306 nundefsym = 0;
308 extsyms = raa_init();
309 strs = saa_init(1L);
311 /* string table starts with a zero byte - don't ask why */
312 saa_wbytes(strs, &zero, sizeof(char));
313 strslen = 1;
316 static void sect_write(struct section *sect,
317 const uint8_t *data, uint32_t len)
319 saa_wbytes(sect->data, data, len);
320 sect->size += len;
323 static void add_reloc(struct section *sect, int32_t section,
324 int pcrel, int bytes)
326 struct reloc *r;
327 int32_t fi;
329 /* NeXT as puts relocs in reversed order (address-wise) into the
330 ** files, so we do the same, doesn't seem to make much of a
331 ** difference either way */
332 r = nasm_malloc(sizeof(struct reloc));
333 r->next = sect->relocs;
334 sect->relocs = r;
336 /* the current end of the section will be the symbol's address for
337 ** now, might have to be fixed by macho_fixup_relocs() later on. make
338 ** sure we don't make the symbol scattered by setting the highest
339 ** bit by accident */
340 r->addr = sect->size & ~R_SCATTERED;
341 r->ext = 0;
342 r->pcrel = pcrel;
344 /* match byte count 1, 2, 4 to length codes 0, 1, 2 respectively */
345 r->length = bytes >> 1;
347 /* vanilla relocation (GENERIC_RELOC_VANILLA) */
348 r->type = 0;
350 if (section == NO_SEG) {
351 /* absolute local symbol if no section index given */
352 r->snum = R_ABS;
353 } else {
354 fi = get_section_fileindex_by_index(section);
356 if (fi == NO_SECT) {
357 /* external symbol if no section with that index known,
358 ** symbol number was saved in macho_symdef() */
359 r->snum = raa_read(extsyms, section);
360 r->ext = 1;
361 } else {
362 /* local symbol in section fi */
363 r->snum = fi;
367 ++sect->nreloc;
370 static void macho_output(int32_t secto, const void *data,
371 enum out_type type, uint64_t size,
372 int32_t section, int32_t wrt)
374 struct section *s, *sbss;
375 int32_t addr;
376 uint8_t mydata[4], *p;
378 if (wrt != NO_SEG) {
379 wrt = NO_SEG;
380 nasm_error(ERR_NONFATAL, "WRT not supported by Mach-O output format");
381 /* continue to do _something_ */
384 if (secto == NO_SEG) {
385 if (type != OUT_RESERVE)
386 nasm_error(ERR_NONFATAL, "attempt to assemble code in "
387 "[ABSOLUTE] space");
389 return;
392 s = get_section_by_index(secto);
394 if (s == NULL) {
395 nasm_error(ERR_WARNING, "attempt to assemble code in"
396 " section %d: defaulting to `.text'", secto);
397 s = get_section_by_name("__TEXT", "__text");
399 /* should never happen */
400 if (s == NULL)
401 nasm_error(ERR_PANIC, "text section not found");
404 sbss = get_section_by_name("__DATA", "__bss");
406 if (s == sbss && type != OUT_RESERVE) {
407 nasm_error(ERR_WARNING, "attempt to initialize memory in the"
408 " BSS section: ignored");
409 s->size += realsize(type, size);
410 return;
413 switch (type) {
414 case OUT_RESERVE:
415 if (s != sbss) {
416 nasm_error(ERR_WARNING, "uninitialized space declared in"
417 " %s section: zeroing",
418 get_section_name_by_index(secto));
420 sect_write(s, NULL, size);
421 } else
422 s->size += size;
424 break;
426 case OUT_RAWDATA:
427 if (section != NO_SEG)
428 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
430 sect_write(s, data, size);
431 break;
433 case OUT_ADDRESS:
435 int asize = abs(size);
437 addr = *(int64_t *)data;
439 if (section != NO_SEG) {
440 if (section % 2) {
441 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
442 " section base references");
443 } else
444 add_reloc(s, section, 0, asize);
447 p = mydata;
448 WRITEADDR(p, addr, asize);
449 sect_write(s, mydata, asize);
450 break;
453 case OUT_REL2ADR:
454 if (section == secto)
455 nasm_error(ERR_PANIC, "intra-section OUT_REL2ADR");
457 if (section != NO_SEG && section % 2) {
458 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
459 " section base references");
460 } else
461 add_reloc(s, section, 1, 2);
463 p = mydata;
464 WRITESHORT(p, *(int32_t *)data - (size + s->size));
465 sect_write(s, mydata, 2L);
466 break;
468 case OUT_REL4ADR:
469 if (section == secto)
470 nasm_error(ERR_PANIC, "intra-section OUT_REL4ADR");
472 if (section != NO_SEG && section % 2) {
473 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
474 " section base references");
475 } else
476 add_reloc(s, section, 1, 4);
478 p = mydata;
479 WRITELONG(p, *(int32_t *)data - (size + s->size));
480 sect_write(s, mydata, 4L);
481 break;
483 default:
484 nasm_error(ERR_PANIC, "unknown output type?");
485 break;
489 static int32_t macho_section(char *name, int pass, int *bits)
491 int32_t index, originalIndex;
492 char *sectionAttributes;
493 struct sectmap *sm;
494 struct section *s;
496 (void)pass;
498 /* Default to 32 bits. */
499 if (!name) {
500 *bits = 32;
501 name = ".text";
502 sectionAttributes = NULL;
503 } else {
504 sectionAttributes = name;
505 name = nasm_strsep(&sectionAttributes, " \t");
508 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
509 /* make lookup into section name translation table */
510 if (!strcmp(name, sm->nasmsect)) {
511 char *currentAttribute;
513 /* try to find section with that name */
514 originalIndex = index = get_section_index_by_name(sm->segname,
515 sm->sectname);
517 /* create it if it doesn't exist yet */
518 if (index == -1) {
519 s = *sectstail = nasm_malloc(sizeof(struct section));
520 s->next = NULL;
521 sectstail = &s->next;
523 s->data = saa_init(1L);
524 s->index = seg_alloc();
525 s->relocs = NULL;
526 s->align = -1;
528 xstrncpy(s->segname, sm->segname);
529 xstrncpy(s->sectname, sm->sectname);
530 s->size = 0;
531 s->nreloc = 0;
532 s->flags = sm->flags;
534 index = s->index;
535 } else {
536 s = get_section_by_index(index);
539 while ((NULL != sectionAttributes)
540 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
541 if (0 != *currentAttribute) {
542 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
543 char *end;
544 int newAlignment, value;
546 value = strtoul(currentAttribute + 6, (char**)&end, 0);
547 newAlignment = alignlog2_32(value);
549 if (0 != *end) {
550 nasm_error(ERR_PANIC,
551 "unknown or missing alignment value \"%s\" "
552 "specified for section \"%s\"",
553 currentAttribute + 6,
554 name);
555 return NO_SEG;
556 } else if (0 > newAlignment) {
557 nasm_error(ERR_PANIC,
558 "alignment of %d (for section \"%s\") is not "
559 "a power of two",
560 value,
561 name);
562 return NO_SEG;
565 if ((-1 != originalIndex)
566 && (s->align != newAlignment)
567 && (s->align != -1)) {
568 nasm_error(ERR_PANIC,
569 "section \"%s\" has already been specified "
570 "with alignment %d, conflicts with new "
571 "alignment of %d",
572 name,
573 (1 << s->align),
574 value);
575 return NO_SEG;
578 s->align = newAlignment;
579 } else if (!nasm_stricmp("data", currentAttribute)) {
580 /* Do nothing; 'data' is implicit */
581 } else {
582 nasm_error(ERR_PANIC,
583 "unknown section attribute %s for section %s",
584 currentAttribute,
585 name);
586 return NO_SEG;
591 return index;
595 nasm_error(ERR_PANIC, "invalid section name %s", name);
596 return NO_SEG;
599 static void macho_symdef(char *name, int32_t section, int64_t offset,
600 int is_global, char *special)
602 struct symbol *sym;
604 if (special) {
605 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
606 "not support any special symbol types");
607 return;
610 if (is_global == 3) {
611 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
612 "(yet) support forward reference fixups.");
613 return;
616 sym = *symstail = nasm_malloc(sizeof(struct symbol));
617 sym->next = NULL;
618 symstail = &sym->next;
620 sym->name = name;
621 sym->strx = strslen;
622 sym->type = 0;
623 sym->desc = 0;
624 sym->value = offset;
625 sym->initial_snum = -1;
627 /* external and common symbols get N_EXT */
628 if (is_global != 0)
629 sym->type |= N_EXT;
631 if (section == NO_SEG) {
632 /* symbols in no section get absolute */
633 sym->type |= N_ABS;
634 sym->sect = NO_SECT;
635 } else {
636 sym->type |= N_SECT;
638 /* get the in-file index of the section the symbol was defined in */
639 sym->sect = get_section_fileindex_by_index(section);
641 if (sym->sect == NO_SECT) {
642 /* remember symbol number of references to external
643 ** symbols, this works because every external symbol gets
644 ** its own section number allocated internally by nasm and
645 ** can so be used as a key */
646 extsyms = raa_write(extsyms, section, nsyms);
647 sym->initial_snum = nsyms;
649 switch (is_global) {
650 case 1:
651 case 2:
652 /* there isn't actually a difference between global
653 ** and common symbols, both even have their size in
654 ** sym->value */
655 sym->type = N_EXT;
656 break;
658 default:
659 /* give an error on unfound section if it's not an
660 ** external or common symbol (assemble_file() does a
661 ** seg_alloc() on every call for them) */
662 nasm_error(ERR_PANIC, "in-file index for section %d not found",
663 section);
668 ++nsyms;
671 static void macho_sectalign(int32_t seg, unsigned int value)
673 struct section *s;
675 list_for_each(s, sects) {
676 if (s->index == seg)
677 break;
680 if (!s || !is_power2(value))
681 return;
683 value = alignlog2_32(value);
684 if (s->align < (int)value)
685 s->align = value;
688 static int32_t macho_segbase(int32_t section)
690 return section;
693 static void macho_filename(char *inname, char *outname)
695 standard_extension(inname, outname, ".o");
698 extern macros_t macho_stdmac[];
700 /* Comparison function for qsort symbol layout. */
701 static int layout_compare (const struct symbol **s1,
702 const struct symbol **s2)
704 return (strcmp ((*s1)->name, (*s2)->name));
707 /* The native assembler does a few things in a similar function
709 * Remove temporary labels
710 * Sort symbols according to local, external, undefined (by name)
711 * Order the string table
713 We do not remove temporary labels right now.
715 numsyms is the total number of symbols we have. strtabsize is the
716 number entries in the string table. */
718 static void macho_layout_symbols (uint32_t *numsyms,
719 uint32_t *strtabsize)
721 struct symbol *sym, **symp;
722 uint32_t i,j;
724 *numsyms = 0;
725 *strtabsize = sizeof (char);
727 symp = &syms;
729 while ((sym = *symp)) {
730 /* Undefined symbols are now external. */
731 if (sym->type == N_UNDF)
732 sym->type |= N_EXT;
734 if ((sym->type & N_EXT) == 0) {
735 sym->snum = *numsyms;
736 *numsyms = *numsyms + 1;
737 nlocalsym++;
739 else {
740 if ((sym->type & N_TYPE) != N_UNDF)
741 nextdefsym++;
742 else
743 nundefsym++;
745 /* If we handle debug info we'll want
746 to check for it here instead of just
747 adding the symbol to the string table. */
748 sym->strx = *strtabsize;
749 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
750 *strtabsize += strlen(sym->name) + 1;
752 symp = &(sym->next);
755 /* Next, sort the symbols. Most of this code is a direct translation from
756 the Apple cctools symbol layout. We need to keep compatibility with that. */
757 /* Set the indexes for symbol groups into the symbol table */
758 ilocalsym = 0;
759 iextdefsym = nlocalsym;
760 iundefsym = nlocalsym + nextdefsym;
762 /* allocate arrays for sorting externals by name */
763 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
764 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
766 i = 0;
767 j = 0;
769 symp = &syms;
771 while ((sym = *symp)) {
773 if((sym->type & N_EXT) == 0) {
774 sym->strx = *strtabsize;
775 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
776 *strtabsize += strlen(sym->name) + 1;
778 else {
779 if((sym->type & N_TYPE) != N_UNDF)
780 extdefsyms[i++] = sym;
781 else
782 undefsyms[j++] = sym;
784 symp = &(sym->next);
787 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
788 (int (*)(const void *, const void *))layout_compare);
789 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
790 (int (*)(const void *, const void *))layout_compare);
792 for(i = 0; i < nextdefsym; i++) {
793 extdefsyms[i]->snum = *numsyms;
794 *numsyms += 1;
796 for(j = 0; j < nundefsym; j++) {
797 undefsyms[j]->snum = *numsyms;
798 *numsyms += 1;
802 /* Calculate some values we'll need for writing later. */
804 static void macho_calculate_sizes (void)
806 struct section *s;
808 /* count sections and calculate in-memory and in-file offsets */
809 for (s = sects; s != NULL; s = s->next) {
810 uint32_t pad = 0;
812 /* zerofill sections aren't actually written to the file */
813 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
814 seg_filesize += s->size;
816 /* recalculate segment address based on alignment and vm size */
817 s->addr = seg_vmsize;
818 /* we need section alignment to calculate final section address */
819 if (s->align == -1)
820 s->align = DEFAULT_SECTION_ALIGNMENT;
821 if(s->align) {
822 uint32_t newaddr = ALIGN(s->addr, 1 << s->align);
823 pad = newaddr - s->addr;
824 s->addr = newaddr;
827 seg_vmsize += s->size + pad;
828 ++seg_nsects;
831 /* calculate size of all headers, load commands and sections to
832 ** get a pointer to the start of all the raw data */
833 if (seg_nsects > 0) {
834 ++head_ncmds;
835 head_sizeofcmds +=
836 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
839 if (nsyms > 0) {
840 ++head_ncmds;
841 head_sizeofcmds += MACHO_SYMCMD_SIZE;
845 /* Write out the header information for the file. */
847 static void macho_write_header (void)
849 fwriteint32_t(MH_MAGIC, ofile); /* magic */
850 fwriteint32_t(CPU_TYPE_I386, ofile); /* CPU type */
851 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
852 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
853 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
854 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
855 fwriteint32_t(0, ofile); /* no flags */
858 /* Write out the segment load command at offset. */
860 static uint32_t macho_write_segment (uint32_t offset)
862 uint32_t rel_base = alignint32_t (offset + seg_filesize);
863 uint32_t s_reloff = 0;
864 struct section *s;
866 fwriteint32_t(LC_SEGMENT, ofile); /* cmd == LC_SEGMENT */
868 /* size of load command including section load commands */
869 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
870 MACHO_SECTCMD_SIZE, ofile);
872 /* in an MH_OBJECT file all sections are in one unnamed (name
873 ** all zeros) segment */
874 fwritezero(16, ofile);
875 fwriteint32_t(0, ofile); /* in-memory offset */
876 fwriteint32_t(seg_vmsize, ofile); /* in-memory size */
877 fwriteint32_t(offset, ofile); /* in-file offset to data */
878 fwriteint32_t(seg_filesize, ofile); /* in-file size */
879 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
880 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
881 fwriteint32_t(seg_nsects, ofile); /* number of sections */
882 fwriteint32_t(0, ofile); /* no flags */
884 /* emit section headers */
885 for (s = sects; s != NULL; s = s->next) {
886 nasm_write(s->sectname, sizeof(s->sectname), ofile);
887 nasm_write(s->segname, sizeof(s->segname), ofile);
888 fwriteint32_t(s->addr, ofile);
889 fwriteint32_t(s->size, ofile);
891 /* dummy data for zerofill sections or proper values */
892 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
893 fwriteint32_t(offset, ofile);
894 /* Write out section alignment, as a power of two.
895 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
896 if (s->align == -1)
897 s->align = DEFAULT_SECTION_ALIGNMENT;
898 fwriteint32_t(s->align, ofile);
899 /* To be compatible with cctools as we emit
900 a zero reloff if we have no relocations. */
901 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
902 fwriteint32_t(s->nreloc, ofile);
904 offset += s->size;
905 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
906 } else {
907 fwriteint32_t(0, ofile);
908 fwriteint32_t(0, ofile);
909 fwriteint32_t(0, ofile);
910 fwriteint32_t(0, ofile);
913 fwriteint32_t(s->flags, ofile); /* flags */
914 fwriteint32_t(0, ofile); /* reserved */
915 fwriteint32_t(0, ofile); /* reserved */
918 rel_padcnt = rel_base - offset;
919 offset = rel_base + s_reloff;
921 return offset;
924 /* For a given chain of relocs r, write out the entire relocation
925 chain to the object file. */
927 static void macho_write_relocs (struct reloc *r)
929 while (r) {
930 uint32_t word2;
932 fwriteint32_t(r->addr, ofile); /* reloc offset */
934 word2 = r->snum;
935 word2 |= r->pcrel << 24;
936 word2 |= r->length << 25;
937 word2 |= r->ext << 27;
938 word2 |= r->type << 28;
939 fwriteint32_t(word2, ofile); /* reloc data */
941 r = r->next;
945 /* Write out the section data. */
946 static void macho_write_section (void)
948 struct section *s, *s2;
949 struct reloc *r;
950 uint8_t fi, *p, *q, blk[4];
951 int32_t l;
953 for (s = sects; s != NULL; s = s->next) {
954 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
955 continue;
957 /* no padding needs to be done to the sections */
959 /* Like a.out Mach-O references things in the data or bss
960 * sections by addresses which are actually relative to the
961 * start of the _text_ section, in the _file_. See outaout.c
962 * for more information. */
963 saa_rewind(s->data);
964 for (r = s->relocs; r != NULL; r = r->next) {
965 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
966 p = q = blk;
967 l = *p++;
969 /* get offset based on relocation type */
970 if (r->length > 0) {
971 l += ((int32_t)*p++) << 8;
973 if (r->length == 2) {
974 l += ((int32_t)*p++) << 16;
975 l += ((int32_t)*p++) << 24;
979 /* If the relocation is internal add to the current section
980 offset. Otherwise the only value we need is the symbol
981 offset which we already have. The linker takes care
982 of the rest of the address. */
983 if (!r->ext) {
984 /* generate final address by section address and offset */
985 for (s2 = sects, fi = 1;
986 s2 != NULL; s2 = s2->next, fi++) {
987 if (fi == r->snum) {
988 l += s2->addr;
989 break;
994 /* write new offset back */
995 if (r->length == 2)
996 WRITELONG(q, l);
997 else if (r->length == 1)
998 WRITESHORT(q, l);
999 else
1000 *q++ = l & 0xFF;
1002 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1005 /* dump the section data to file */
1006 saa_fpwrite(s->data, ofile);
1009 /* pad last section up to reloc entries on int32_t boundary */
1010 fwritezero(rel_padcnt, ofile);
1012 /* emit relocation entries */
1013 for (s = sects; s != NULL; s = s->next)
1014 macho_write_relocs (s->relocs);
1017 /* Write out the symbol table. We should already have sorted this
1018 before now. */
1019 static void macho_write_symtab (void)
1021 struct symbol *sym;
1022 struct section *s;
1023 int32_t fi;
1024 uint32_t i;
1026 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1028 for (sym = syms; sym != NULL; sym = sym->next) {
1029 if ((sym->type & N_EXT) == 0) {
1030 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1031 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1032 fwrite(&sym->sect, 1, 1, ofile); /* section */
1033 fwriteint16_t(sym->desc, ofile); /* description */
1035 /* Fix up the symbol value now that we know the final section
1036 sizes. */
1037 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1038 for (s = sects, fi = 1; s != NULL; s = s->next, fi++) {
1039 if (fi == sym->sect) {
1040 sym->value += s->addr;
1041 break;
1046 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1050 for (i = 0; i < nextdefsym; i++) {
1051 sym = extdefsyms[i];
1052 fwriteint32_t(sym->strx, ofile);
1053 nasm_write(&sym->type, 1, ofile); /* symbol type */
1054 nasm_write(&sym->sect, 1, ofile); /* section */
1055 fwriteint16_t(sym->desc, ofile); /* description */
1057 /* Fix up the symbol value now that we know the final section
1058 sizes. */
1059 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1060 for (s = sects, fi = 1;
1061 s != NULL && fi < sym->sect; s = s->next, ++fi)
1062 sym->value += s->size;
1065 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1068 for (i = 0; i < nundefsym; i++) {
1069 sym = undefsyms[i];
1070 fwriteint32_t(sym->strx, ofile);
1071 nasm_write(&sym->type, 1, ofile); /* symbol type */
1072 nasm_write(&sym->sect, 1, ofile); /* section */
1073 fwriteint16_t(sym->desc, ofile); /* description */
1075 /* Fix up the symbol value now that we know the final section
1076 sizes. */
1077 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1078 for (s = sects, fi = 1;
1079 s != NULL && fi < sym->sect; s = s->next, ++fi)
1080 sym->value += s->size;
1083 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1087 /* Fixup the snum in the relocation entries, we should be
1088 doing this only for externally undefined symbols. */
1089 static void macho_fixup_relocs (struct reloc *r)
1091 struct symbol *sym;
1092 uint32_t i;
1094 while (r != NULL) {
1095 if (r->ext) {
1096 for (i = 0; i < nundefsym; i++) {
1097 sym = undefsyms[i];
1098 if (sym->initial_snum == r->snum) {
1099 r->snum = sym->snum;
1100 break;
1104 r = r->next;
1108 /* Write out the object file. */
1110 static void macho_write (void)
1112 uint32_t offset = 0;
1114 /* mach-o object file structure:
1116 ** mach header
1117 ** uint32_t magic
1118 ** int cpu type
1119 ** int cpu subtype
1120 ** uint32_t mach file type
1121 ** uint32_t number of load commands
1122 ** uint32_t size of all load commands
1123 ** (includes section struct size of segment command)
1124 ** uint32_t flags
1126 ** segment command
1127 ** uint32_t command type == LC_SEGMENT
1128 ** uint32_t size of load command
1129 ** (including section load commands)
1130 ** char[16] segment name
1131 ** uint32_t in-memory offset
1132 ** uint32_t in-memory size
1133 ** uint32_t in-file offset to data area
1134 ** uint32_t in-file size
1135 ** (in-memory size excluding zerofill sections)
1136 ** int maximum vm protection
1137 ** int initial vm protection
1138 ** uint32_t number of sections
1139 ** uint32_t flags
1141 ** section commands
1142 ** char[16] section name
1143 ** char[16] segment name
1144 ** uint32_t in-memory offset
1145 ** uint32_t in-memory size
1146 ** uint32_t in-file offset
1147 ** uint32_t alignment
1148 ** (irrelevant in MH_OBJECT)
1149 ** uint32_t in-file offset of relocation entires
1150 ** uint32_t number of relocations
1151 ** uint32_t flags
1152 ** uint32_t reserved
1153 ** uint32_t reserved
1155 ** symbol table command
1156 ** uint32_t command type == LC_SYMTAB
1157 ** uint32_t size of load command
1158 ** uint32_t symbol table offset
1159 ** uint32_t number of symbol table entries
1160 ** uint32_t string table offset
1161 ** uint32_t string table size
1163 ** raw section data
1165 ** padding to int32_t boundary
1167 ** relocation data (struct reloc)
1168 ** int32_t offset
1169 ** uint data (symbolnum, pcrel, length, extern, type)
1171 ** symbol table data (struct nlist)
1172 ** int32_t string table entry number
1173 ** uint8_t type
1174 ** (extern, absolute, defined in section)
1175 ** uint8_t section
1176 ** (0 for global symbols, section number of definition (>= 1, <=
1177 ** 254) for local symbols, size of variable for common symbols
1178 ** [type == extern])
1179 ** int16_t description
1180 ** (for stab debugging format)
1181 ** uint32_t value (i.e. file offset) of symbol or stab offset
1183 ** string table data
1184 ** list of null-terminated strings
1187 /* Emit the Mach-O header. */
1188 macho_write_header();
1190 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1192 /* emit the segment load command */
1193 if (seg_nsects > 0)
1194 offset = macho_write_segment (offset);
1195 else
1196 nasm_error(ERR_WARNING, "no sections?");
1198 if (nsyms > 0) {
1199 /* write out symbol command */
1200 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1201 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1202 fwriteint32_t(offset, ofile); /* symbol table offset */
1203 fwriteint32_t(nsyms, ofile); /* number of symbol
1204 ** table entries */
1206 offset += nsyms * MACHO_NLIST_SIZE;
1207 fwriteint32_t(offset, ofile); /* string table offset */
1208 fwriteint32_t(strslen, ofile); /* string table size */
1211 /* emit section data */
1212 if (seg_nsects > 0)
1213 macho_write_section ();
1215 /* emit symbol table if we have symbols */
1216 if (nsyms > 0)
1217 macho_write_symtab ();
1219 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1221 /* emit string table */
1222 saa_fpwrite(strs, ofile);
1224 /* We do quite a bit here, starting with finalizing all of the data
1225 for the object file, writing, and then freeing all of the data from
1226 the file. */
1228 static void macho_cleanup(int debuginfo)
1230 struct section *s;
1231 struct reloc *r;
1232 struct symbol *sym;
1234 (void)debuginfo;
1236 /* Sort all symbols. */
1237 macho_layout_symbols (&nsyms, &strslen);
1239 /* Fixup relocation entries */
1240 for (s = sects; s != NULL; s = s->next) {
1241 macho_fixup_relocs (s->relocs);
1244 /* First calculate and finalize needed values. */
1245 macho_calculate_sizes();
1246 macho_write();
1248 /* free up everything */
1249 while (sects->next) {
1250 s = sects;
1251 sects = sects->next;
1253 saa_free(s->data);
1254 while (s->relocs != NULL) {
1255 r = s->relocs;
1256 s->relocs = s->relocs->next;
1257 nasm_free(r);
1260 nasm_free(s);
1263 saa_free(strs);
1264 raa_free(extsyms);
1266 if (syms) {
1267 while (syms->next) {
1268 sym = syms;
1269 syms = syms->next;
1271 nasm_free (sym);
1276 /* Debugging routines. */
1277 static void debug_reloc (struct reloc *r)
1279 fprintf (stdout, "reloc:\n");
1280 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1281 fprintf (stdout, "\tsnum: %d\n", r->snum);
1282 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1283 fprintf (stdout, "\tlength: %d\n", r->length);
1284 fprintf (stdout, "\text: %d\n", r->ext);
1285 fprintf (stdout, "\ttype: %d\n", r->type);
1288 static void debug_section_relocs (struct section *s)
1290 struct reloc *r = s->relocs;
1292 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1294 while (r != NULL) {
1295 debug_reloc (r);
1296 r = r->next;
1300 struct ofmt of_macho32 = {
1301 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1302 "macho32",
1304 null_debug_arr,
1305 &null_debug_form,
1306 macho_stdmac,
1307 macho_init,
1308 null_setinfo,
1309 macho_output,
1310 macho_symdef,
1311 macho_section,
1312 macho_sectalign,
1313 macho_segbase,
1314 null_directive,
1315 macho_filename,
1316 macho_cleanup
1319 #endif
1322 * Local Variables:
1323 * mode:c
1324 * c-basic-offset:4
1325 * End:
1327 * end of file */