macho: make a bunch of global variables static
[nasm.git] / output / outmac32.c
blob0f545108f94e1f660d2cd8d68db025d5b25a3305
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 static uint32_t head_ncmds = 0;
214 static uint32_t head_sizeofcmds = 0;
215 static uint32_t seg_filesize = 0;
216 static uint32_t seg_vmsize = 0;
217 static uint32_t seg_nsects = 0;
218 static 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[8], *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 memset(mydata, 0, sizeof(mydata));
415 switch (type) {
416 case OUT_RESERVE:
417 if (s != sbss) {
418 nasm_error(ERR_WARNING, "uninitialized space declared in"
419 " %s section: zeroing",
420 get_section_name_by_index(secto));
422 sect_write(s, NULL, size);
423 } else
424 s->size += size;
426 break;
428 case OUT_RAWDATA:
429 if (section != NO_SEG)
430 nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
432 sect_write(s, data, size);
433 break;
435 case OUT_ADDRESS:
437 int asize = abs((int)size);
439 addr = *(int64_t *)data;
441 if (section != NO_SEG) {
442 if (section % 2) {
443 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
444 " section base references");
445 } else
446 add_reloc(s, section, 0, asize);
449 p = mydata;
450 WRITEADDR(p, addr, asize);
451 sect_write(s, mydata, asize);
452 break;
455 case OUT_REL2ADR:
456 if (section == secto)
457 nasm_error(ERR_PANIC, "intra-section OUT_REL2ADR");
459 if (section != NO_SEG && section % 2) {
460 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
461 " section base references");
462 } else
463 add_reloc(s, section, 1, 2);
465 p = mydata;
466 WRITESHORT(p, *(int32_t *)data - (size + s->size));
467 sect_write(s, mydata, 2L);
468 break;
470 case OUT_REL4ADR:
471 if (section == secto)
472 nasm_error(ERR_PANIC, "intra-section OUT_REL4ADR");
474 if (section != NO_SEG && section % 2) {
475 nasm_error(ERR_NONFATAL, "Mach-O format does not support"
476 " section base references");
477 } else
478 add_reloc(s, section, 1, 4);
480 p = mydata;
481 WRITELONG(p, *(int32_t *)data - (size + s->size));
482 sect_write(s, mydata, 4L);
483 break;
485 default:
486 nasm_error(ERR_PANIC, "unknown output type?");
487 break;
491 static int32_t macho_section(char *name, int pass, int *bits)
493 int32_t index, originalIndex;
494 char *sectionAttributes;
495 struct sectmap *sm;
496 struct section *s;
498 (void)pass;
500 /* Default to 32 bits. */
501 if (!name) {
502 *bits = 32;
503 name = ".text";
504 sectionAttributes = NULL;
505 } else {
506 sectionAttributes = name;
507 name = nasm_strsep(&sectionAttributes, " \t");
510 for (sm = sectmap; sm->nasmsect != NULL; ++sm) {
511 /* make lookup into section name translation table */
512 if (!strcmp(name, sm->nasmsect)) {
513 char *currentAttribute;
515 /* try to find section with that name */
516 originalIndex = index = get_section_index_by_name(sm->segname,
517 sm->sectname);
519 /* create it if it doesn't exist yet */
520 if (index == -1) {
521 s = *sectstail = nasm_malloc(sizeof(struct section));
522 s->next = NULL;
523 sectstail = &s->next;
525 s->data = saa_init(1L);
526 s->index = seg_alloc();
527 s->relocs = NULL;
528 s->align = -1;
530 xstrncpy(s->segname, sm->segname);
531 xstrncpy(s->sectname, sm->sectname);
532 s->size = 0;
533 s->nreloc = 0;
534 s->flags = sm->flags;
536 index = s->index;
537 } else {
538 s = get_section_by_index(index);
541 while ((NULL != sectionAttributes)
542 && (currentAttribute = nasm_strsep(&sectionAttributes, " \t"))) {
543 if (0 != *currentAttribute) {
544 if (!nasm_strnicmp("align=", currentAttribute, 6)) {
545 char *end;
546 int newAlignment, value;
548 value = strtoul(currentAttribute + 6, (char**)&end, 0);
549 newAlignment = alignlog2_32(value);
551 if (0 != *end) {
552 nasm_error(ERR_PANIC,
553 "unknown or missing alignment value \"%s\" "
554 "specified for section \"%s\"",
555 currentAttribute + 6,
556 name);
557 return NO_SEG;
558 } else if (0 > newAlignment) {
559 nasm_error(ERR_PANIC,
560 "alignment of %d (for section \"%s\") is not "
561 "a power of two",
562 value,
563 name);
564 return NO_SEG;
567 if ((-1 != originalIndex)
568 && (s->align != newAlignment)
569 && (s->align != -1)) {
570 nasm_error(ERR_PANIC,
571 "section \"%s\" has already been specified "
572 "with alignment %d, conflicts with new "
573 "alignment of %d",
574 name,
575 (1 << s->align),
576 value);
577 return NO_SEG;
580 s->align = newAlignment;
581 } else if (!nasm_stricmp("data", currentAttribute)) {
582 /* Do nothing; 'data' is implicit */
583 } else {
584 nasm_error(ERR_PANIC,
585 "unknown section attribute %s for section %s",
586 currentAttribute,
587 name);
588 return NO_SEG;
593 return index;
597 nasm_error(ERR_PANIC, "invalid section name %s", name);
598 return NO_SEG;
601 static void macho_symdef(char *name, int32_t section, int64_t offset,
602 int is_global, char *special)
604 struct symbol *sym;
606 if (special) {
607 nasm_error(ERR_NONFATAL, "The Mach-O output format does "
608 "not support any special symbol types");
609 return;
612 if (is_global == 3) {
613 nasm_error(ERR_NONFATAL, "The Mach-O format does not "
614 "(yet) support forward reference fixups.");
615 return;
618 sym = *symstail = nasm_malloc(sizeof(struct symbol));
619 sym->next = NULL;
620 symstail = &sym->next;
622 sym->name = name;
623 sym->strx = strslen;
624 sym->type = 0;
625 sym->desc = 0;
626 sym->value = offset;
627 sym->initial_snum = -1;
629 /* external and common symbols get N_EXT */
630 if (is_global != 0)
631 sym->type |= N_EXT;
633 if (section == NO_SEG) {
634 /* symbols in no section get absolute */
635 sym->type |= N_ABS;
636 sym->sect = NO_SECT;
637 } else {
638 sym->type |= N_SECT;
640 /* get the in-file index of the section the symbol was defined in */
641 sym->sect = get_section_fileindex_by_index(section);
643 if (sym->sect == NO_SECT) {
644 /* remember symbol number of references to external
645 ** symbols, this works because every external symbol gets
646 ** its own section number allocated internally by nasm and
647 ** can so be used as a key */
648 extsyms = raa_write(extsyms, section, nsyms);
649 sym->initial_snum = nsyms;
651 switch (is_global) {
652 case 1:
653 case 2:
654 /* there isn't actually a difference between global
655 ** and common symbols, both even have their size in
656 ** sym->value */
657 sym->type = N_EXT;
658 break;
660 default:
661 /* give an error on unfound section if it's not an
662 ** external or common symbol (assemble_file() does a
663 ** seg_alloc() on every call for them) */
664 nasm_error(ERR_PANIC, "in-file index for section %d not found",
665 section);
670 ++nsyms;
673 static void macho_sectalign(int32_t seg, unsigned int value)
675 struct section *s;
677 list_for_each(s, sects) {
678 if (s->index == seg)
679 break;
682 if (!s || !is_power2(value))
683 return;
685 value = alignlog2_32(value);
686 if (s->align < (int)value)
687 s->align = value;
690 static int32_t macho_segbase(int32_t section)
692 return section;
695 static void macho_filename(char *inname, char *outname)
697 standard_extension(inname, outname, ".o");
700 extern macros_t macho_stdmac[];
702 /* Comparison function for qsort symbol layout. */
703 static int layout_compare (const struct symbol **s1,
704 const struct symbol **s2)
706 return (strcmp ((*s1)->name, (*s2)->name));
709 /* The native assembler does a few things in a similar function
711 * Remove temporary labels
712 * Sort symbols according to local, external, undefined (by name)
713 * Order the string table
715 We do not remove temporary labels right now.
717 numsyms is the total number of symbols we have. strtabsize is the
718 number entries in the string table. */
720 static void macho_layout_symbols (uint32_t *numsyms,
721 uint32_t *strtabsize)
723 struct symbol *sym, **symp;
724 uint32_t i,j;
726 *numsyms = 0;
727 *strtabsize = sizeof (char);
729 symp = &syms;
731 while ((sym = *symp)) {
732 /* Undefined symbols are now external. */
733 if (sym->type == N_UNDF)
734 sym->type |= N_EXT;
736 if ((sym->type & N_EXT) == 0) {
737 sym->snum = *numsyms;
738 *numsyms = *numsyms + 1;
739 nlocalsym++;
741 else {
742 if ((sym->type & N_TYPE) != N_UNDF)
743 nextdefsym++;
744 else
745 nundefsym++;
747 /* If we handle debug info we'll want
748 to check for it here instead of just
749 adding the symbol to the string table. */
750 sym->strx = *strtabsize;
751 saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
752 *strtabsize += strlen(sym->name) + 1;
754 symp = &(sym->next);
757 /* Next, sort the symbols. Most of this code is a direct translation from
758 the Apple cctools symbol layout. We need to keep compatibility with that. */
759 /* Set the indexes for symbol groups into the symbol table */
760 ilocalsym = 0;
761 iextdefsym = nlocalsym;
762 iundefsym = nlocalsym + nextdefsym;
764 /* allocate arrays for sorting externals by name */
765 extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
766 undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
768 i = 0;
769 j = 0;
771 symp = &syms;
773 while ((sym = *symp)) {
775 if((sym->type & N_EXT) == 0) {
776 sym->strx = *strtabsize;
777 saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
778 *strtabsize += strlen(sym->name) + 1;
780 else {
781 if((sym->type & N_TYPE) != N_UNDF)
782 extdefsyms[i++] = sym;
783 else
784 undefsyms[j++] = sym;
786 symp = &(sym->next);
789 qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
790 (int (*)(const void *, const void *))layout_compare);
791 qsort(undefsyms, nundefsym, sizeof(struct symbol *),
792 (int (*)(const void *, const void *))layout_compare);
794 for(i = 0; i < nextdefsym; i++) {
795 extdefsyms[i]->snum = *numsyms;
796 *numsyms += 1;
798 for(j = 0; j < nundefsym; j++) {
799 undefsyms[j]->snum = *numsyms;
800 *numsyms += 1;
804 /* Calculate some values we'll need for writing later. */
806 static void macho_calculate_sizes (void)
808 struct section *s;
810 /* count sections and calculate in-memory and in-file offsets */
811 for (s = sects; s != NULL; s = s->next) {
812 uint32_t pad = 0;
814 /* zerofill sections aren't actually written to the file */
815 if ((s->flags & SECTION_TYPE) != S_ZEROFILL)
816 seg_filesize += s->size;
818 /* recalculate segment address based on alignment and vm size */
819 s->addr = seg_vmsize;
820 /* we need section alignment to calculate final section address */
821 if (s->align == -1)
822 s->align = DEFAULT_SECTION_ALIGNMENT;
823 if(s->align) {
824 uint32_t newaddr = ALIGN(s->addr, 1 << s->align);
825 pad = newaddr - s->addr;
826 s->addr = newaddr;
829 seg_vmsize += s->size + pad;
830 ++seg_nsects;
833 /* calculate size of all headers, load commands and sections to
834 ** get a pointer to the start of all the raw data */
835 if (seg_nsects > 0) {
836 ++head_ncmds;
837 head_sizeofcmds +=
838 MACHO_SEGCMD_SIZE + seg_nsects * MACHO_SECTCMD_SIZE;
841 if (nsyms > 0) {
842 ++head_ncmds;
843 head_sizeofcmds += MACHO_SYMCMD_SIZE;
847 /* Write out the header information for the file. */
849 static void macho_write_header (void)
851 fwriteint32_t(MH_MAGIC, ofile); /* magic */
852 fwriteint32_t(CPU_TYPE_I386, ofile); /* CPU type */
853 fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
854 fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
855 fwriteint32_t(head_ncmds, ofile); /* number of load commands */
856 fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
857 fwriteint32_t(0, ofile); /* no flags */
860 /* Write out the segment load command at offset. */
862 static uint32_t macho_write_segment (uint32_t offset)
864 uint32_t rel_base = alignint32_t (offset + seg_filesize);
865 uint32_t s_reloff = 0;
866 struct section *s;
868 fwriteint32_t(LC_SEGMENT, ofile); /* cmd == LC_SEGMENT */
870 /* size of load command including section load commands */
871 fwriteint32_t(MACHO_SEGCMD_SIZE + seg_nsects *
872 MACHO_SECTCMD_SIZE, ofile);
874 /* in an MH_OBJECT file all sections are in one unnamed (name
875 ** all zeros) segment */
876 fwritezero(16, ofile);
877 fwriteint32_t(0, ofile); /* in-memory offset */
878 fwriteint32_t(seg_vmsize, ofile); /* in-memory size */
879 fwriteint32_t(offset, ofile); /* in-file offset to data */
880 fwriteint32_t(seg_filesize, ofile); /* in-file size */
881 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
882 fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
883 fwriteint32_t(seg_nsects, ofile); /* number of sections */
884 fwriteint32_t(0, ofile); /* no flags */
886 /* emit section headers */
887 for (s = sects; s != NULL; s = s->next) {
888 nasm_write(s->sectname, sizeof(s->sectname), ofile);
889 nasm_write(s->segname, sizeof(s->segname), ofile);
890 fwriteint32_t(s->addr, ofile);
891 fwriteint32_t(s->size, ofile);
893 /* dummy data for zerofill sections or proper values */
894 if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
895 fwriteint32_t(offset, ofile);
896 /* Write out section alignment, as a power of two.
897 e.g. 32-bit word alignment would be 2 (2^^2 = 4). */
898 if (s->align == -1)
899 s->align = DEFAULT_SECTION_ALIGNMENT;
900 fwriteint32_t(s->align, ofile);
901 /* To be compatible with cctools as we emit
902 a zero reloff if we have no relocations. */
903 fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
904 fwriteint32_t(s->nreloc, ofile);
906 offset += s->size;
907 s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
908 } else {
909 fwriteint32_t(0, ofile);
910 fwriteint32_t(0, ofile);
911 fwriteint32_t(0, ofile);
912 fwriteint32_t(0, ofile);
915 fwriteint32_t(s->flags, ofile); /* flags */
916 fwriteint32_t(0, ofile); /* reserved */
917 fwriteint32_t(0, ofile); /* reserved */
920 rel_padcnt = rel_base - offset;
921 offset = rel_base + s_reloff;
923 return offset;
926 /* For a given chain of relocs r, write out the entire relocation
927 chain to the object file. */
929 static void macho_write_relocs (struct reloc *r)
931 while (r) {
932 uint32_t word2;
934 fwriteint32_t(r->addr, ofile); /* reloc offset */
936 word2 = r->snum;
937 word2 |= r->pcrel << 24;
938 word2 |= r->length << 25;
939 word2 |= r->ext << 27;
940 word2 |= r->type << 28;
941 fwriteint32_t(word2, ofile); /* reloc data */
943 r = r->next;
947 /* Write out the section data. */
948 static void macho_write_section (void)
950 struct section *s, *s2;
951 struct reloc *r;
952 uint8_t fi, *p, *q, blk[4];
953 int32_t l;
955 for (s = sects; s != NULL; s = s->next) {
956 if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
957 continue;
959 /* no padding needs to be done to the sections */
961 /* Like a.out Mach-O references things in the data or bss
962 * sections by addresses which are actually relative to the
963 * start of the _text_ section, in the _file_. See outaout.c
964 * for more information. */
965 saa_rewind(s->data);
966 for (r = s->relocs; r != NULL; r = r->next) {
967 saa_fread(s->data, r->addr, blk, (int32_t)r->length << 1);
968 p = q = blk;
969 l = *p++;
971 /* get offset based on relocation type */
972 if (r->length > 0) {
973 l += ((int32_t)*p++) << 8;
975 if (r->length == 2) {
976 l += ((int32_t)*p++) << 16;
977 l += ((int32_t)*p++) << 24;
981 /* If the relocation is internal add to the current section
982 offset. Otherwise the only value we need is the symbol
983 offset which we already have. The linker takes care
984 of the rest of the address. */
985 if (!r->ext) {
986 /* generate final address by section address and offset */
987 for (s2 = sects, fi = 1;
988 s2 != NULL; s2 = s2->next, fi++) {
989 if (fi == r->snum) {
990 l += s2->addr;
991 break;
996 /* write new offset back */
997 if (r->length == 2)
998 WRITELONG(q, l);
999 else if (r->length == 1)
1000 WRITESHORT(q, l);
1001 else
1002 *q++ = l & 0xFF;
1004 saa_fwrite(s->data, r->addr, blk, (int32_t)r->length << 1);
1007 /* dump the section data to file */
1008 saa_fpwrite(s->data, ofile);
1011 /* pad last section up to reloc entries on int32_t boundary */
1012 fwritezero(rel_padcnt, ofile);
1014 /* emit relocation entries */
1015 for (s = sects; s != NULL; s = s->next)
1016 macho_write_relocs (s->relocs);
1019 /* Write out the symbol table. We should already have sorted this
1020 before now. */
1021 static void macho_write_symtab (void)
1023 struct symbol *sym;
1024 struct section *s;
1025 int32_t fi;
1026 uint32_t i;
1028 /* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
1030 for (sym = syms; sym != NULL; sym = sym->next) {
1031 if ((sym->type & N_EXT) == 0) {
1032 fwriteint32_t(sym->strx, ofile); /* string table entry number */
1033 fwrite(&sym->type, 1, 1, ofile); /* symbol type */
1034 fwrite(&sym->sect, 1, 1, ofile); /* section */
1035 fwriteint16_t(sym->desc, ofile); /* description */
1037 /* Fix up the symbol value now that we know the final section
1038 sizes. */
1039 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1040 for (s = sects, fi = 1; s != NULL; s = s->next, fi++) {
1041 if (fi == sym->sect) {
1042 sym->value += s->addr;
1043 break;
1048 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1052 for (i = 0; i < nextdefsym; i++) {
1053 sym = extdefsyms[i];
1054 fwriteint32_t(sym->strx, ofile);
1055 nasm_write(&sym->type, 1, ofile); /* symbol type */
1056 nasm_write(&sym->sect, 1, ofile); /* section */
1057 fwriteint16_t(sym->desc, ofile); /* description */
1059 /* Fix up the symbol value now that we know the final section
1060 sizes. */
1061 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1062 for (s = sects, fi = 1;
1063 s != NULL && fi < sym->sect; s = s->next, ++fi)
1064 sym->value += s->size;
1067 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1070 for (i = 0; i < nundefsym; i++) {
1071 sym = undefsyms[i];
1072 fwriteint32_t(sym->strx, ofile);
1073 nasm_write(&sym->type, 1, ofile); /* symbol type */
1074 nasm_write(&sym->sect, 1, ofile); /* section */
1075 fwriteint16_t(sym->desc, ofile); /* description */
1077 /* Fix up the symbol value now that we know the final section
1078 sizes. */
1079 if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
1080 for (s = sects, fi = 1;
1081 s != NULL && fi < sym->sect; s = s->next, ++fi)
1082 sym->value += s->size;
1085 fwriteint32_t(sym->value, ofile); /* value (i.e. offset) */
1089 /* Fixup the snum in the relocation entries, we should be
1090 doing this only for externally undefined symbols. */
1091 static void macho_fixup_relocs (struct reloc *r)
1093 struct symbol *sym;
1094 uint32_t i;
1096 while (r != NULL) {
1097 if (r->ext) {
1098 for (i = 0; i < nundefsym; i++) {
1099 sym = undefsyms[i];
1100 if (sym->initial_snum == r->snum) {
1101 r->snum = sym->snum;
1102 break;
1106 r = r->next;
1110 /* Write out the object file. */
1112 static void macho_write (void)
1114 uint32_t offset = 0;
1116 /* mach-o object file structure:
1118 ** mach header
1119 ** uint32_t magic
1120 ** int cpu type
1121 ** int cpu subtype
1122 ** uint32_t mach file type
1123 ** uint32_t number of load commands
1124 ** uint32_t size of all load commands
1125 ** (includes section struct size of segment command)
1126 ** uint32_t flags
1128 ** segment command
1129 ** uint32_t command type == LC_SEGMENT
1130 ** uint32_t size of load command
1131 ** (including section load commands)
1132 ** char[16] segment name
1133 ** uint32_t in-memory offset
1134 ** uint32_t in-memory size
1135 ** uint32_t in-file offset to data area
1136 ** uint32_t in-file size
1137 ** (in-memory size excluding zerofill sections)
1138 ** int maximum vm protection
1139 ** int initial vm protection
1140 ** uint32_t number of sections
1141 ** uint32_t flags
1143 ** section commands
1144 ** char[16] section name
1145 ** char[16] segment name
1146 ** uint32_t in-memory offset
1147 ** uint32_t in-memory size
1148 ** uint32_t in-file offset
1149 ** uint32_t alignment
1150 ** (irrelevant in MH_OBJECT)
1151 ** uint32_t in-file offset of relocation entires
1152 ** uint32_t number of relocations
1153 ** uint32_t flags
1154 ** uint32_t reserved
1155 ** uint32_t reserved
1157 ** symbol table command
1158 ** uint32_t command type == LC_SYMTAB
1159 ** uint32_t size of load command
1160 ** uint32_t symbol table offset
1161 ** uint32_t number of symbol table entries
1162 ** uint32_t string table offset
1163 ** uint32_t string table size
1165 ** raw section data
1167 ** padding to int32_t boundary
1169 ** relocation data (struct reloc)
1170 ** int32_t offset
1171 ** uint data (symbolnum, pcrel, length, extern, type)
1173 ** symbol table data (struct nlist)
1174 ** int32_t string table entry number
1175 ** uint8_t type
1176 ** (extern, absolute, defined in section)
1177 ** uint8_t section
1178 ** (0 for global symbols, section number of definition (>= 1, <=
1179 ** 254) for local symbols, size of variable for common symbols
1180 ** [type == extern])
1181 ** int16_t description
1182 ** (for stab debugging format)
1183 ** uint32_t value (i.e. file offset) of symbol or stab offset
1185 ** string table data
1186 ** list of null-terminated strings
1189 /* Emit the Mach-O header. */
1190 macho_write_header();
1192 offset = MACHO_HEADER_SIZE + head_sizeofcmds;
1194 /* emit the segment load command */
1195 if (seg_nsects > 0)
1196 offset = macho_write_segment (offset);
1197 else
1198 nasm_error(ERR_WARNING, "no sections?");
1200 if (nsyms > 0) {
1201 /* write out symbol command */
1202 fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
1203 fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
1204 fwriteint32_t(offset, ofile); /* symbol table offset */
1205 fwriteint32_t(nsyms, ofile); /* number of symbol
1206 ** table entries */
1208 offset += nsyms * MACHO_NLIST_SIZE;
1209 fwriteint32_t(offset, ofile); /* string table offset */
1210 fwriteint32_t(strslen, ofile); /* string table size */
1213 /* emit section data */
1214 if (seg_nsects > 0)
1215 macho_write_section ();
1217 /* emit symbol table if we have symbols */
1218 if (nsyms > 0)
1219 macho_write_symtab ();
1221 /* we don't need to pad here since MACHO_NLIST_SIZE == 12 */
1223 /* emit string table */
1224 saa_fpwrite(strs, ofile);
1226 /* We do quite a bit here, starting with finalizing all of the data
1227 for the object file, writing, and then freeing all of the data from
1228 the file. */
1230 static void macho_cleanup(int debuginfo)
1232 struct section *s;
1233 struct reloc *r;
1234 struct symbol *sym;
1236 (void)debuginfo;
1238 /* Sort all symbols. */
1239 macho_layout_symbols (&nsyms, &strslen);
1241 /* Fixup relocation entries */
1242 for (s = sects; s != NULL; s = s->next) {
1243 macho_fixup_relocs (s->relocs);
1246 /* First calculate and finalize needed values. */
1247 macho_calculate_sizes();
1248 macho_write();
1250 /* free up everything */
1251 while (sects->next) {
1252 s = sects;
1253 sects = sects->next;
1255 saa_free(s->data);
1256 while (s->relocs != NULL) {
1257 r = s->relocs;
1258 s->relocs = s->relocs->next;
1259 nasm_free(r);
1262 nasm_free(s);
1265 saa_free(strs);
1266 raa_free(extsyms);
1268 if (syms) {
1269 while (syms->next) {
1270 sym = syms;
1271 syms = syms->next;
1273 nasm_free (sym);
1278 /* Debugging routines. */
1279 static void debug_reloc (struct reloc *r)
1281 fprintf (stdout, "reloc:\n");
1282 fprintf (stdout, "\taddr: %"PRId32"\n", r->addr);
1283 fprintf (stdout, "\tsnum: %d\n", r->snum);
1284 fprintf (stdout, "\tpcrel: %d\n", r->pcrel);
1285 fprintf (stdout, "\tlength: %d\n", r->length);
1286 fprintf (stdout, "\text: %d\n", r->ext);
1287 fprintf (stdout, "\ttype: %d\n", r->type);
1290 static void debug_section_relocs (struct section *s)
1292 struct reloc *r = s->relocs;
1294 fprintf (stdout, "relocs for section %s:\n\n", s->sectname);
1296 while (r != NULL) {
1297 debug_reloc (r);
1298 r = r->next;
1302 struct ofmt of_macho32 = {
1303 "NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X (i386) object files",
1304 "macho32",
1307 null_debug_arr,
1308 &null_debug_form,
1309 macho_stdmac,
1310 macho_init,
1311 null_setinfo,
1312 macho_output,
1313 macho_symdef,
1314 macho_section,
1315 macho_sectalign,
1316 macho_segbase,
1317 null_directive,
1318 macho_filename,
1319 macho_cleanup
1322 #endif
1325 * Local Variables:
1326 * mode:c
1327 * c-basic-offset:4
1328 * End:
1330 * end of file */