2009-06-29 08:25:15 +08:00
|
|
|
/* ----------------------------------------------------------------------- *
|
2016-02-12 19:04:41 +08:00
|
|
|
*
|
2018-05-31 05:43:46 +08:00
|
|
|
* Copyright 1996-2018 The NASM Authors - All Rights Reserved
|
2009-06-29 08:25:15 +08:00
|
|
|
* See the file AUTHORS included with the NASM distribution for
|
|
|
|
* the specific copyright holders.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following
|
|
|
|
* conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
2016-02-12 19:04:41 +08:00
|
|
|
*
|
2009-06-29 08:25:15 +08:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
|
|
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
|
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* ----------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
/*
|
2016-02-13 13:18:12 +08:00
|
|
|
* outmacho.c output routines for the Netwide Assembler to produce
|
2016-02-13 05:36:37 +08:00
|
|
|
* NeXTstep/OpenStep/Rhapsody/Darwin/MacOS X object files
|
2005-09-23 05:22:33 +08:00
|
|
|
*/
|
|
|
|
|
2007-10-03 12:53:51 +08:00
|
|
|
#include "compiler.h"
|
|
|
|
|
2018-12-28 04:37:25 +08:00
|
|
|
#include "nctype.h"
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
#include "nasm.h"
|
|
|
|
#include "nasmlib.h"
|
2017-09-28 04:34:42 +08:00
|
|
|
#include "ilog2.h"
|
2017-03-15 09:43:57 +08:00
|
|
|
#include "labels.h"
|
2017-03-08 11:23:03 +08:00
|
|
|
#include "error.h"
|
2008-06-10 08:32:43 +08:00
|
|
|
#include "saa.h"
|
2008-06-10 08:40:16 +08:00
|
|
|
#include "raa.h"
|
2016-02-17 14:15:03 +08:00
|
|
|
#include "rbtree.h"
|
2018-06-15 08:14:36 +08:00
|
|
|
#include "hashtbl.h"
|
2016-05-26 03:06:29 +08:00
|
|
|
#include "outform.h"
|
|
|
|
#include "outlib.h"
|
2017-04-08 17:37:11 +08:00
|
|
|
#include "ver.h"
|
|
|
|
#include "dwarf.h"
|
2018-05-02 22:55:39 +08:00
|
|
|
#include "macho.h"
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
#if defined(OF_MACHO) || defined(OF_MACHO64)
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* Mach-O in-file header structure sizes */
|
2016-02-12 19:04:41 +08:00
|
|
|
#define MACHO_HEADER_SIZE 28
|
|
|
|
#define MACHO_SEGCMD_SIZE 56
|
|
|
|
#define MACHO_SECTCMD_SIZE 68
|
|
|
|
#define MACHO_SYMCMD_SIZE 24
|
|
|
|
#define MACHO_NLIST_SIZE 12
|
|
|
|
#define MACHO_RELINFO_SIZE 8
|
|
|
|
|
|
|
|
#define MACHO_HEADER64_SIZE 32
|
|
|
|
#define MACHO_SEGCMD64_SIZE 72
|
|
|
|
#define MACHO_SECTCMD64_SIZE 80
|
|
|
|
#define MACHO_NLIST64_SIZE 16
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
/* Mach-O relocations numbers */
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
#define VM_PROT_DEFAULT (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
|
|
|
|
#define VM_PROT_ALL (VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE)
|
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
/* Our internal relocation types */
|
|
|
|
enum reltype {
|
|
|
|
RL_ABS, /* Absolute relocation */
|
|
|
|
RL_REL, /* Relative relocation */
|
|
|
|
RL_TLV, /* Thread local */
|
2016-02-22 12:56:19 +08:00
|
|
|
RL_BRANCH, /* Relative direct branch */
|
2016-02-17 14:15:03 +08:00
|
|
|
RL_SUB, /* X86_64_RELOC_SUBTRACT */
|
|
|
|
RL_GOT, /* X86_64_RELOC_GOT */
|
2016-03-02 14:43:51 +08:00
|
|
|
RL_GOTLOAD /* X86_64_RELOC_GOT_LOAD */
|
2016-02-17 14:15:03 +08:00
|
|
|
};
|
|
|
|
#define RL_MAX_32 RL_TLV
|
|
|
|
#define RL_MAX_64 RL_GOTLOAD
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
struct macho_fmt {
|
|
|
|
uint32_t ptrsize; /* Pointer size in bytes */
|
|
|
|
uint32_t mh_magic; /* Which magic number to use */
|
|
|
|
uint32_t cpu_type; /* Which CPU type */
|
|
|
|
uint32_t lc_segment; /* Which segment load command */
|
|
|
|
uint32_t header_size; /* Header size */
|
|
|
|
uint32_t segcmd_size; /* Segment command size */
|
|
|
|
uint32_t sectcmd_size; /* Section command size */
|
|
|
|
uint32_t nlist_size; /* Nlist (symbol) size */
|
2016-02-17 14:15:03 +08:00
|
|
|
enum reltype maxreltype; /* Maximum entry in enum reltype permitted */
|
|
|
|
uint32_t reloc_abs; /* Absolute relocation type */
|
|
|
|
uint32_t reloc_rel; /* Relative relocation type */
|
|
|
|
uint32_t reloc_tlv; /* Thread local relocation type */
|
2017-06-23 14:36:34 +08:00
|
|
|
bool forcesym; /* Always use "external" (symbol-relative) relocations */
|
2016-02-12 19:04:41 +08:00
|
|
|
};
|
|
|
|
|
2016-02-18 05:28:19 +08:00
|
|
|
static struct macho_fmt fmt;
|
2016-02-12 19:04:41 +08:00
|
|
|
|
|
|
|
static void fwriteptr(uint64_t data, FILE * fp)
|
|
|
|
{
|
2016-02-18 05:28:19 +08:00
|
|
|
fwriteaddr(data, fmt.ptrsize, fp);
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
struct section {
|
|
|
|
/* nasm internal data */
|
2005-09-23 05:41:17 +08:00
|
|
|
struct section *next;
|
|
|
|
struct SAA *data;
|
2018-06-15 08:14:36 +08:00
|
|
|
int32_t index; /* Main section index */
|
|
|
|
int32_t subsection; /* Current subsection index */
|
2016-02-17 09:38:58 +08:00
|
|
|
int32_t fileindex;
|
2005-09-23 05:41:17 +08:00
|
|
|
struct reloc *relocs;
|
2017-06-23 14:36:34 +08:00
|
|
|
struct rbtree *syms[2]; /* All/global symbols symbols in section */
|
2005-12-09 08:27:26 +08:00
|
|
|
int align;
|
2018-05-31 05:43:46 +08:00
|
|
|
bool by_name; /* This section was specified by full MachO name */
|
2018-06-15 08:14:36 +08:00
|
|
|
char namestr[34]; /* segment,section as a C string */
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* data that goes into the file */
|
2018-05-31 05:43:46 +08:00
|
|
|
char sectname[16]; /* what this section is called */
|
|
|
|
char segname[16]; /* segment this section will be in */
|
2009-07-09 12:04:52 +08:00
|
|
|
uint64_t addr; /* in-memory address (subject to alignment) */
|
|
|
|
uint64_t size; /* in-memory and -file size */
|
2016-02-12 09:51:37 +08:00
|
|
|
uint64_t offset; /* in-file offset */
|
2016-02-03 09:15:03 +08:00
|
|
|
uint32_t pad; /* padding bytes before section */
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t nreloc; /* relocation entry count */
|
|
|
|
uint32_t flags; /* type and attributes (masked) */
|
2016-02-03 09:15:03 +08:00
|
|
|
uint32_t extreloc; /* external relocations */
|
2005-09-23 05:22:33 +08:00
|
|
|
};
|
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
#define S_NASM_TYPE_MASK 0x800004ff /* we consider these bits "section type" */
|
|
|
|
|
|
|
|
/* fake section for absolute symbols, *not* part of the section linked list */
|
2016-02-23 16:13:49 +08:00
|
|
|
static struct section absolute_sect;
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
struct reloc {
|
|
|
|
/* nasm internal data */
|
|
|
|
struct reloc *next;
|
|
|
|
|
|
|
|
/* data that goes into the file */
|
2016-02-23 16:13:49 +08:00
|
|
|
int32_t addr; /* op's offset in section */
|
|
|
|
uint32_t snum:24, /* contains symbol index if
|
|
|
|
** ext otherwise in-file
|
|
|
|
** section number */
|
2005-10-08 06:30:56 +08:00
|
|
|
pcrel:1, /* relative relocation */
|
2009-07-09 12:04:52 +08:00
|
|
|
length:2, /* 0=byte, 1=word, 2=int32_t, 3=int64_t */
|
2005-10-08 06:30:56 +08:00
|
|
|
ext:1, /* external symbol referenced */
|
2009-07-09 12:04:52 +08:00
|
|
|
type:4; /* reloc type */
|
2005-09-23 05:22:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct symbol {
|
2005-10-08 06:30:56 +08:00
|
|
|
/* nasm internal data */
|
2017-06-23 14:36:34 +08:00
|
|
|
struct rbtree symv[2]; /* All/global symbol rbtrees; "key" contains the
|
2016-02-17 14:15:03 +08:00
|
|
|
symbol offset. */
|
2005-10-08 06:30:56 +08:00
|
|
|
struct symbol *next; /* next symbol in the list */
|
2007-04-14 00:47:53 +08:00
|
|
|
char *name; /* name of this symbol */
|
2016-02-17 14:15:03 +08:00
|
|
|
int32_t initial_snum; /* symbol number used above in reloc */
|
|
|
|
int32_t snum; /* true snum for reloc */
|
2005-10-08 06:30:56 +08:00
|
|
|
|
|
|
|
/* data that goes into the file */
|
2016-02-17 14:15:03 +08:00
|
|
|
uint32_t strx; /* string table index */
|
|
|
|
uint8_t type; /* symbol type */
|
|
|
|
uint8_t sect; /* NO_SECT or section number */
|
|
|
|
uint16_t desc; /* for stab debugging, 0 for us */
|
2005-09-23 05:22:33 +08:00
|
|
|
};
|
|
|
|
|
2005-12-09 08:27:26 +08:00
|
|
|
#define DEFAULT_SECTION_ALIGNMENT 0 /* byte (i.e. no) alignment */
|
|
|
|
|
2016-02-12 09:51:37 +08:00
|
|
|
static struct section *sects, **sectstail, **sectstab;
|
2005-10-08 06:30:56 +08:00
|
|
|
static struct symbol *syms, **symstail;
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t nsyms;
|
2005-10-08 06:30:56 +08:00
|
|
|
|
|
|
|
/* These variables are set by macho_layout_symbols() to organize
|
|
|
|
the symbol table and string table in order the dynamic linker
|
|
|
|
expects. They are then used in macho_write() to put out the
|
|
|
|
symbols and strings in that order.
|
|
|
|
|
|
|
|
The order of the symbol table is:
|
|
|
|
local symbols
|
|
|
|
defined external symbols (sorted by name)
|
|
|
|
undefined external symbols (sorted by name)
|
|
|
|
|
|
|
|
The order of the string table is:
|
|
|
|
strings for external symbols
|
|
|
|
strings for local symbols
|
|
|
|
*/
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t ilocalsym = 0;
|
|
|
|
static uint32_t iextdefsym = 0;
|
|
|
|
static uint32_t iundefsym = 0;
|
|
|
|
static uint32_t nlocalsym;
|
|
|
|
static uint32_t nextdefsym;
|
|
|
|
static uint32_t nundefsym;
|
2005-10-08 06:30:56 +08:00
|
|
|
static struct symbol **extdefsyms = NULL;
|
|
|
|
static struct symbol **undefsyms = NULL;
|
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
static struct RAA *extsyms;
|
|
|
|
static struct SAA *strs;
|
2007-04-12 10:40:54 +08:00
|
|
|
static uint32_t strslen;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
/* Global file information. This should be cleaned up into either
|
|
|
|
a structure or as function arguments. */
|
2016-02-12 17:23:53 +08:00
|
|
|
static uint32_t head_ncmds = 0;
|
|
|
|
static uint32_t head_sizeofcmds = 0;
|
2017-03-08 14:43:00 +08:00
|
|
|
static uint32_t head_flags = 0;
|
2016-02-12 17:23:53 +08:00
|
|
|
static uint64_t seg_filesize = 0;
|
|
|
|
static uint64_t seg_vmsize = 0;
|
|
|
|
static uint32_t seg_nsects = 0;
|
|
|
|
static uint64_t rel_padcnt = 0;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
2017-09-28 05:42:08 +08:00
|
|
|
/*
|
|
|
|
* Functions for handling fixed-length zero-padded string
|
|
|
|
* fields, that may or may not be null-terminated.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Copy a string into a zero-padded fixed-length field */
|
|
|
|
#define xstrncpy(xdst, xsrc) strncpy(xdst, xsrc, sizeof(xdst))
|
|
|
|
|
|
|
|
/* Compare a fixed-length field with a string */
|
|
|
|
#define xstrncmp(xdst, xsrc) strncmp(xdst, xsrc, sizeof(xdst))
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
#define alignint32_t(x) \
|
2016-02-12 19:04:41 +08:00
|
|
|
ALIGN(x, sizeof(int32_t)) /* align x to int32_t boundary */
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2009-07-09 12:04:52 +08:00
|
|
|
#define alignint64_t(x) \
|
2016-02-12 19:04:41 +08:00
|
|
|
ALIGN(x, sizeof(int64_t)) /* align x to int64_t boundary */
|
|
|
|
|
|
|
|
#define alignptr(x) \
|
2016-02-18 05:28:19 +08:00
|
|
|
ALIGN(x, fmt.ptrsize) /* align x to output format width */
|
2009-07-09 12:04:52 +08:00
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
static struct hash_table section_by_name;
|
|
|
|
static struct RAA *section_by_index;
|
|
|
|
|
|
|
|
static struct section * never_null
|
|
|
|
find_or_add_section(const char *segname, const char *sectname)
|
2005-09-23 05:41:17 +08:00
|
|
|
{
|
2018-06-15 08:14:36 +08:00
|
|
|
struct hash_insert hi;
|
|
|
|
void **sp;
|
2005-09-23 05:22:33 +08:00
|
|
|
struct section *s;
|
2018-06-15 08:14:36 +08:00
|
|
|
char sect[34];
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
snprintf(sect, sizeof sect, "%-16s,%-16s", segname, sectname);
|
|
|
|
|
|
|
|
sp = hash_find(§ion_by_name, sect, &hi);
|
|
|
|
if (sp)
|
|
|
|
return (struct section *)(*sp);
|
|
|
|
|
|
|
|
s = nasm_zalloc(sizeof *s);
|
|
|
|
xstrncpy(s->segname, segname);
|
|
|
|
xstrncpy(s->sectname, sectname);
|
|
|
|
xstrncpy(s->namestr, sect);
|
|
|
|
hash_add(&hi, s->namestr, s);
|
|
|
|
|
|
|
|
s->index = s->subsection = seg_alloc();
|
|
|
|
section_by_index = raa_write_ptr(section_by_index, s->index >> 1, s);
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
static inline bool is_new_section(const struct section *s)
|
2005-09-23 05:41:17 +08:00
|
|
|
{
|
2018-06-15 08:14:36 +08:00
|
|
|
return !s->data;
|
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
static struct section *get_section_by_name(const char *segname,
|
|
|
|
const char *sectname)
|
|
|
|
{
|
|
|
|
char sect[34];
|
|
|
|
void **sp;
|
2018-05-31 05:43:46 +08:00
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
snprintf(sect, sizeof sect, "%-16s,%-16s", segname, sectname);
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
sp = hash_find(§ion_by_name, sect, NULL);
|
|
|
|
return sp ? (struct section *)(*sp) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct section *get_section_by_index(int32_t index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= SEG_ABS || (index & 1))
|
|
|
|
return NULL;
|
|
|
|
|
2018-06-19 07:00:48 +08:00
|
|
|
return raa_read_ptr(section_by_index, index >> 1);
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2017-04-27 03:13:08 +08:00
|
|
|
struct dir_list {
|
|
|
|
struct dir_list *next;
|
|
|
|
struct dir_list *last;
|
|
|
|
const char *dir_name;
|
|
|
|
uint32_t dir;
|
|
|
|
};
|
|
|
|
|
2017-04-08 17:25:14 +08:00
|
|
|
struct file_list {
|
|
|
|
struct file_list *next;
|
|
|
|
struct file_list *last;
|
2017-04-27 03:13:08 +08:00
|
|
|
const char *file_name;
|
2017-04-08 17:25:14 +08:00
|
|
|
uint32_t file;
|
2017-04-27 03:13:08 +08:00
|
|
|
struct dir_list *dir;
|
2017-04-08 17:25:14 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dw_sect_list {
|
|
|
|
struct SAA *psaa;
|
|
|
|
int32_t section;
|
|
|
|
uint32_t line;
|
|
|
|
uint64_t offset;
|
|
|
|
uint32_t file;
|
|
|
|
struct dw_sect_list *next;
|
|
|
|
struct dw_sect_list *last;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct section_info {
|
|
|
|
uint64_t size;
|
|
|
|
int32_t secto;
|
|
|
|
};
|
|
|
|
|
2017-04-08 17:45:54 +08:00
|
|
|
#define DW_LN_BASE (-5)
|
|
|
|
#define DW_LN_RANGE 14
|
|
|
|
#define DW_OPCODE_BASE 13
|
|
|
|
#define DW_MAX_LN (DW_LN_BASE + DW_LN_RANGE)
|
|
|
|
#define DW_MAX_SP_OPCODE 256
|
|
|
|
|
2017-04-27 03:13:08 +08:00
|
|
|
static struct file_list *dw_head_file = 0, *dw_cur_file = 0, **dw_last_file_next = NULL;
|
|
|
|
static struct dir_list *dw_head_dir = 0, **dw_last_dir_next = NULL;
|
2017-04-08 17:25:14 +08:00
|
|
|
static struct dw_sect_list *dw_head_sect = 0, *dw_cur_sect = 0, *dw_last_sect = 0;
|
2017-04-27 03:13:08 +08:00
|
|
|
static uint32_t cur_line = 0, dw_num_files = 0, dw_num_dirs = 0, dw_num_sects = 0;
|
2017-04-08 17:25:14 +08:00
|
|
|
static bool dbg_immcall = false;
|
2017-04-25 12:08:23 +08:00
|
|
|
static const char *module_name = NULL;
|
2017-04-08 17:25:14 +08:00
|
|
|
|
2009-07-09 12:04:52 +08:00
|
|
|
/*
|
|
|
|
* Special section numbers which are used to define Mach-O special
|
|
|
|
* symbols, which can be used with WRT to provide PIC relocation
|
|
|
|
* types.
|
|
|
|
*/
|
2016-02-17 14:15:03 +08:00
|
|
|
static int32_t macho_tlvp_sect;
|
2009-07-09 12:04:52 +08:00
|
|
|
static int32_t macho_gotpcrel_sect;
|
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
static void macho_init(void)
|
2005-09-23 05:22:33 +08:00
|
|
|
{
|
2018-02-25 20:50:35 +08:00
|
|
|
module_name = inname;
|
2005-09-23 05:22:33 +08:00
|
|
|
sects = NULL;
|
|
|
|
sectstail = §s;
|
|
|
|
|
2016-02-23 16:13:49 +08:00
|
|
|
/* Fake section for absolute symbols */
|
|
|
|
absolute_sect.index = NO_SEG;
|
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
syms = NULL;
|
|
|
|
symstail = &syms;
|
2005-09-23 05:22:33 +08:00
|
|
|
nsyms = 0;
|
2005-10-08 06:30:56 +08:00
|
|
|
nlocalsym = 0;
|
|
|
|
nextdefsym = 0;
|
|
|
|
nundefsym = 0;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
extsyms = raa_init();
|
|
|
|
strs = saa_init(1L);
|
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
section_by_index = raa_init();
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
/* string table starts with a zero byte so index 0 is an empty string */
|
|
|
|
saa_wbytes(strs, zero_buffer, 1);
|
2005-09-23 05:22:33 +08:00
|
|
|
strslen = 1;
|
2016-02-17 14:15:03 +08:00
|
|
|
|
|
|
|
/* add special symbol for TLVP */
|
|
|
|
macho_tlvp_sect = seg_alloc() + 1;
|
2018-06-02 09:02:54 +08:00
|
|
|
backend_label("..tlvp", macho_tlvp_sect, 0L);
|
2018-05-31 05:43:46 +08:00
|
|
|
}
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
static void sect_write(struct section *sect,
|
2007-04-12 10:40:54 +08:00
|
|
|
const uint8_t *data, uint32_t len)
|
2005-09-23 05:22:33 +08:00
|
|
|
{
|
|
|
|
saa_wbytes(sect->data, data, len);
|
|
|
|
sect->size += len;
|
|
|
|
}
|
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
/*
|
|
|
|
* Find a suitable global symbol for a ..gotpcrel or ..tlvp reference
|
|
|
|
*/
|
2017-06-23 14:36:34 +08:00
|
|
|
static struct symbol *macho_find_sym(struct section *s, uint64_t offset,
|
|
|
|
bool global, bool exact)
|
2016-02-17 14:15:03 +08:00
|
|
|
{
|
|
|
|
struct rbtree *srb;
|
|
|
|
|
2017-06-23 14:36:34 +08:00
|
|
|
srb = rb_search(s->syms[global], offset);
|
2016-02-17 14:15:03 +08:00
|
|
|
|
|
|
|
if (!srb || (exact && srb->key != offset)) {
|
2017-06-23 14:36:34 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "unable to find a suitable%s%s symbol"
|
2016-02-23 16:13:49 +08:00
|
|
|
" for this reference",
|
2017-06-23 14:36:34 +08:00
|
|
|
global ? " global" : "",
|
|
|
|
s == &absolute_sect ? " absolute " : "");
|
2016-02-17 14:15:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2017-06-23 14:36:34 +08:00
|
|
|
return container_of(srb - global, struct symbol, symv);
|
2016-02-17 14:15:03 +08:00
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
|
2016-02-17 03:30:54 +08:00
|
|
|
static int64_t add_reloc(struct section *sect, int32_t section,
|
2016-02-17 14:15:03 +08:00
|
|
|
int64_t offset,
|
2016-02-17 03:30:54 +08:00
|
|
|
enum reltype reltype, int bytes)
|
2005-09-23 05:41:17 +08:00
|
|
|
{
|
2005-09-23 05:22:33 +08:00
|
|
|
struct reloc *r;
|
2016-02-17 14:15:03 +08:00
|
|
|
struct section *s;
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t fi;
|
2016-02-17 03:30:54 +08:00
|
|
|
int64_t adjust;
|
2016-02-12 19:04:41 +08:00
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
/* Double check this is a valid relocation type for this platform */
|
2016-02-18 05:28:19 +08:00
|
|
|
nasm_assert(reltype <= fmt.maxreltype);
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* the current end of the section will be the symbol's address for
|
2005-10-08 06:30:56 +08:00
|
|
|
** now, might have to be fixed by macho_fixup_relocs() later on. make
|
2005-12-06 07:17:17 +08:00
|
|
|
** sure we don't make the symbol scattered by setting the highest
|
2005-09-23 05:41:17 +08:00
|
|
|
** bit by accident */
|
2016-02-17 14:15:03 +08:00
|
|
|
r = nasm_malloc(sizeof(struct reloc));
|
2005-09-23 05:22:33 +08:00
|
|
|
r->addr = sect->size & ~R_SCATTERED;
|
2009-07-09 12:04:52 +08:00
|
|
|
r->ext = 1;
|
2018-10-09 09:49:54 +08:00
|
|
|
adjust = 0;
|
2009-07-09 12:04:52 +08:00
|
|
|
|
|
|
|
/* match byte count 1, 2, 4, 8 to length codes 0, 1, 2, 3 respectively */
|
2016-02-12 19:04:41 +08:00
|
|
|
r->length = ilog2_32(bytes);
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2009-07-09 12:04:52 +08:00
|
|
|
/* set default relocation values */
|
2016-02-18 05:28:19 +08:00
|
|
|
r->type = fmt.reloc_abs;
|
2016-02-12 19:04:41 +08:00
|
|
|
r->pcrel = 0;
|
|
|
|
r->snum = R_ABS;
|
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
s = get_section_by_index(section);
|
2016-02-17 14:15:03 +08:00
|
|
|
fi = s ? s->fileindex : NO_SECT;
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
/* absolute relocation */
|
|
|
|
switch (reltype) {
|
|
|
|
case RL_ABS:
|
|
|
|
if (section == NO_SEG) {
|
2016-02-13 07:06:23 +08:00
|
|
|
/* absolute (can this even happen?) */
|
|
|
|
r->ext = 0;
|
2016-02-17 14:15:03 +08:00
|
|
|
} else if (fi == NO_SECT) {
|
|
|
|
/* external */
|
|
|
|
r->snum = raa_read(extsyms, section);
|
2016-02-12 19:04:41 +08:00
|
|
|
} else {
|
2016-02-17 14:15:03 +08:00
|
|
|
/* local */
|
|
|
|
r->ext = 0;
|
|
|
|
r->snum = fi;
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RL_REL:
|
2016-02-23 16:13:49 +08:00
|
|
|
case RL_BRANCH:
|
2016-02-18 05:28:19 +08:00
|
|
|
r->type = fmt.reloc_rel;
|
2016-02-12 19:04:41 +08:00
|
|
|
r->pcrel = 1;
|
|
|
|
if (section == NO_SEG) {
|
2018-10-09 09:49:50 +08:00
|
|
|
/* may optionally be converted below by fmt.forcesym */
|
|
|
|
r->ext = 0;
|
2016-02-17 14:15:03 +08:00
|
|
|
} else if (fi == NO_SECT) {
|
|
|
|
/* external */
|
|
|
|
sect->extreloc = 1;
|
|
|
|
r->snum = raa_read(extsyms, section);
|
2016-02-23 16:13:49 +08:00
|
|
|
if (reltype == RL_BRANCH)
|
|
|
|
r->type = X86_64_RELOC_BRANCH;
|
2016-02-12 19:04:41 +08:00
|
|
|
} else {
|
2016-02-17 14:15:03 +08:00
|
|
|
/* local */
|
|
|
|
r->ext = 0;
|
|
|
|
r->snum = fi;
|
2018-08-24 05:13:45 +08:00
|
|
|
if (reltype == RL_BRANCH)
|
|
|
|
r->type = X86_64_RELOC_BRANCH;
|
2009-07-09 12:04:52 +08:00
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
break;
|
|
|
|
|
2018-10-09 09:49:52 +08:00
|
|
|
case RL_SUB: /* obsolete */
|
2019-08-07 10:18:36 +08:00
|
|
|
nasm_warn(WARN_OTHER, "relcation with subtraction"
|
2018-10-09 09:49:52 +08:00
|
|
|
"becomes to be obsolete");
|
|
|
|
r->ext = 0;
|
2016-02-17 14:15:03 +08:00
|
|
|
r->type = X86_64_RELOC_SUBTRACTOR;
|
2016-02-12 19:04:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RL_GOT:
|
2016-02-17 14:15:03 +08:00
|
|
|
r->type = X86_64_RELOC_GOT;
|
|
|
|
goto needsym;
|
2016-02-12 19:04:41 +08:00
|
|
|
|
|
|
|
case RL_GOTLOAD:
|
2016-02-17 14:15:03 +08:00
|
|
|
r->type = X86_64_RELOC_GOT_LOAD;
|
|
|
|
goto needsym;
|
|
|
|
|
|
|
|
case RL_TLV:
|
2016-02-18 05:28:19 +08:00
|
|
|
r->type = fmt.reloc_tlv;
|
2016-02-17 14:15:03 +08:00
|
|
|
goto needsym;
|
|
|
|
|
|
|
|
needsym:
|
2018-10-09 09:49:51 +08:00
|
|
|
r->pcrel = (fmt.ptrsize == 8 ? 1 : 0);
|
2016-02-17 14:15:03 +08:00
|
|
|
if (section == NO_SEG) {
|
|
|
|
nasm_error(ERR_NONFATAL, "Unsupported use of use of WRT");
|
2018-10-09 09:49:51 +08:00
|
|
|
goto bail;
|
2016-02-17 14:15:03 +08:00
|
|
|
} else if (fi == NO_SECT) {
|
|
|
|
/* external */
|
|
|
|
r->snum = raa_read(extsyms, section);
|
|
|
|
} else {
|
2018-10-09 09:49:51 +08:00
|
|
|
/* internal - GOTPCREL doesn't need to be in global */
|
|
|
|
struct symbol *sym = macho_find_sym(s, offset,
|
|
|
|
false, /* reltype != RL_TLV */
|
|
|
|
true);
|
|
|
|
if (!sym) {
|
|
|
|
nasm_error(ERR_NONFATAL, "Symbol for WRT not found");
|
2016-02-17 14:15:03 +08:00
|
|
|
goto bail;
|
2018-10-09 09:49:51 +08:00
|
|
|
}
|
2017-06-23 14:36:34 +08:00
|
|
|
|
2018-10-09 09:49:54 +08:00
|
|
|
adjust -= sym->symv[0].key;
|
2016-02-17 14:15:03 +08:00
|
|
|
r->snum = sym->initial_snum;
|
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
break;
|
|
|
|
}
|
2005-09-23 05:41:17 +08:00
|
|
|
|
2018-10-09 09:49:53 +08:00
|
|
|
/*
|
|
|
|
* For 64-bit Mach-O, force a symbol reference if at all possible
|
|
|
|
* Allow for r->snum == R_ABS by searching absolute_sect
|
|
|
|
*/
|
|
|
|
if (!r->ext && fmt.forcesym) {
|
|
|
|
struct symbol *sym = macho_find_sym(s ? s : &absolute_sect,
|
|
|
|
offset, false, false);
|
2017-06-23 14:36:34 +08:00
|
|
|
if (sym) {
|
2018-10-09 09:49:54 +08:00
|
|
|
adjust -= sym->symv[0].key;
|
2017-06-23 14:36:34 +08:00
|
|
|
r->snum = sym->initial_snum;
|
|
|
|
r->ext = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-09 09:49:54 +08:00
|
|
|
if (r->pcrel)
|
|
|
|
adjust += ((r->ext && fmt.ptrsize == 8) ? bytes : -(int64_t)sect->size);
|
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
/* NeXT as puts relocs in reversed order (address-wise) into the
|
|
|
|
** files, so we do the same, doesn't seem to make much of a
|
|
|
|
** difference either way */
|
|
|
|
r->next = sect->relocs;
|
|
|
|
sect->relocs = r;
|
|
|
|
if (r->ext)
|
|
|
|
sect->extreloc = 1;
|
2016-02-12 19:04:41 +08:00
|
|
|
++sect->nreloc;
|
2016-02-17 14:15:03 +08:00
|
|
|
|
2016-02-17 03:30:54 +08:00
|
|
|
return adjust;
|
2016-02-17 14:15:03 +08:00
|
|
|
|
|
|
|
bail:
|
|
|
|
nasm_free(r);
|
|
|
|
return 0;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
static void macho_output(int32_t secto, const void *data,
|
|
|
|
enum out_type type, uint64_t size,
|
2007-04-12 10:40:54 +08:00
|
|
|
int32_t section, int32_t wrt)
|
2005-09-23 05:22:33 +08:00
|
|
|
{
|
2016-02-17 04:39:26 +08:00
|
|
|
struct section *s;
|
2016-02-17 14:15:03 +08:00
|
|
|
int64_t addr, offset;
|
2016-02-22 12:56:19 +08:00
|
|
|
uint8_t mydata[16], *p;
|
2016-02-17 04:39:26 +08:00
|
|
|
bool is_bss;
|
2016-02-22 12:56:19 +08:00
|
|
|
enum reltype reltype;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
s = get_section_by_index(secto);
|
2018-06-15 08:14:36 +08:00
|
|
|
if (!s) {
|
2019-08-07 10:18:36 +08:00
|
|
|
nasm_warn(WARN_OTHER, "attempt to assemble code in"
|
2005-09-23 05:41:17 +08:00
|
|
|
" section %d: defaulting to `.text'", secto);
|
|
|
|
s = get_section_by_name("__TEXT", "__text");
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
/* should never happen */
|
2018-06-15 08:14:36 +08:00
|
|
|
if (!s)
|
2018-06-16 09:20:17 +08:00
|
|
|
nasm_panic("text section not found");
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:25:14 +08:00
|
|
|
/* debug code generation only for sections tagged with
|
|
|
|
* instruction attribute */
|
|
|
|
if (s->flags & S_ATTR_SOME_INSTRUCTIONS)
|
|
|
|
{
|
|
|
|
struct section_info sinfo;
|
|
|
|
sinfo.size = s->size;
|
|
|
|
sinfo.secto = secto;
|
|
|
|
dfmt->debug_output(0, &sinfo);
|
|
|
|
}
|
|
|
|
|
2016-02-17 04:39:26 +08:00
|
|
|
is_bss = (s->flags & SECTION_TYPE) == S_ZEROFILL;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-17 04:39:26 +08:00
|
|
|
if (is_bss && type != OUT_RESERVE) {
|
2019-08-07 10:18:36 +08:00
|
|
|
nasm_warn(WARN_OTHER, "attempt to initialize memory in "
|
2016-02-17 04:39:26 +08:00
|
|
|
"BSS section: ignored");
|
2018-10-09 09:49:57 +08:00
|
|
|
/* FIXME */
|
2019-08-07 10:18:36 +08:00
|
|
|
nasm_warn(WARN_OTHER, "section size may be negative"
|
2018-10-09 09:49:57 +08:00
|
|
|
"with address symbols");
|
2008-10-28 13:19:59 +08:00
|
|
|
s->size += realsize(type, size);
|
2005-09-23 05:41:17 +08:00
|
|
|
return;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-19 19:12:44 +08:00
|
|
|
memset(mydata, 0, sizeof(mydata));
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
switch (type) {
|
2007-11-10 06:44:02 +08:00
|
|
|
case OUT_RESERVE:
|
2016-02-17 04:39:26 +08:00
|
|
|
if (!is_bss) {
|
2019-08-07 10:37:49 +08:00
|
|
|
nasm_warn(WARN_ZEROING, "uninitialized space declared in"
|
2016-02-17 04:39:26 +08:00
|
|
|
" %s,%s section: zeroing", s->segname, s->sectname);
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
sect_write(s, NULL, size);
|
2005-09-23 05:41:17 +08:00
|
|
|
} else
|
2007-11-10 06:44:02 +08:00
|
|
|
s->size += size;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
break;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
case OUT_RAWDATA:
|
|
|
|
sect_write(s, data, size);
|
2005-09-23 05:41:17 +08:00
|
|
|
break;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
case OUT_ADDRESS:
|
2013-11-27 10:09:56 +08:00
|
|
|
{
|
2016-02-06 03:34:07 +08:00
|
|
|
int asize = abs((int)size);
|
2013-11-27 10:09:56 +08:00
|
|
|
|
2007-11-14 01:37:59 +08:00
|
|
|
addr = *(int64_t *)data;
|
2005-09-23 05:41:17 +08:00
|
|
|
if (section != NO_SEG) {
|
|
|
|
if (section % 2) {
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "Mach-O format does not support"
|
2017-06-23 14:36:34 +08:00
|
|
|
" section base references");
|
2016-02-12 19:04:41 +08:00
|
|
|
} else if (wrt == NO_SEG) {
|
2018-05-09 03:46:55 +08:00
|
|
|
if (fmt.ptrsize == 8 && asize != 8) {
|
2016-02-17 04:39:26 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
|
|
|
"Mach-O 64-bit format does not support"
|
2016-02-12 19:04:41 +08:00
|
|
|
" 32-bit absolute addresses");
|
|
|
|
} else {
|
2018-10-09 09:49:55 +08:00
|
|
|
addr += add_reloc(s, section, addr, RL_ABS, asize);
|
2009-07-09 12:04:52 +08:00
|
|
|
}
|
2018-10-09 09:49:55 +08:00
|
|
|
} else if (wrt == macho_tlvp_sect && fmt.ptrsize != 8 &&
|
|
|
|
asize == (int) fmt.ptrsize) {
|
|
|
|
addr += add_reloc(s, section, addr, RL_TLV, asize);
|
2016-02-12 19:04:41 +08:00
|
|
|
} else {
|
|
|
|
nasm_error(ERR_NONFATAL, "Mach-O format does not support"
|
|
|
|
" this use of WRT");
|
|
|
|
}
|
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
p = mydata;
|
2013-11-27 10:09:56 +08:00
|
|
|
WRITEADDR(p, addr, asize);
|
|
|
|
sect_write(s, mydata, asize);
|
2005-09-23 05:41:17 +08:00
|
|
|
break;
|
2013-11-27 10:09:56 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-05-02 23:07:52 +08:00
|
|
|
case OUT_REL1ADR:
|
2007-11-10 06:44:02 +08:00
|
|
|
case OUT_REL2ADR:
|
2009-07-09 12:04:52 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
p = mydata;
|
2016-02-17 14:15:03 +08:00
|
|
|
offset = *(int64_t *)data;
|
|
|
|
addr = offset - size;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
if (section != NO_SEG && section % 2) {
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "Mach-O format does not support"
|
2016-02-12 19:04:41 +08:00
|
|
|
" section base references");
|
2016-02-18 05:28:19 +08:00
|
|
|
} else if (fmt.ptrsize == 8) {
|
2016-02-12 19:04:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "Unsupported non-32-bit"
|
|
|
|
" Macho-O relocation [2]");
|
|
|
|
} else if (wrt != NO_SEG) {
|
|
|
|
nasm_error(ERR_NONFATAL, "Mach-O format does not support"
|
|
|
|
" this use of WRT");
|
|
|
|
wrt = NO_SEG; /* we can at least _try_ to continue */
|
|
|
|
} else {
|
2018-05-02 23:07:52 +08:00
|
|
|
addr += add_reloc(s, section, addr+size, RL_REL,
|
|
|
|
type == OUT_REL1ADR ? 1 : 2);
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
WRITESHORT(p, addr);
|
2018-05-02 23:07:52 +08:00
|
|
|
sect_write(s, mydata, type == OUT_REL1ADR ? 1 : 2);
|
2005-09-23 05:41:17 +08:00
|
|
|
break;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2007-11-10 06:44:02 +08:00
|
|
|
case OUT_REL4ADR:
|
2018-05-02 23:07:52 +08:00
|
|
|
case OUT_REL8ADR:
|
2016-02-12 19:04:41 +08:00
|
|
|
|
2009-07-09 12:04:52 +08:00
|
|
|
p = mydata;
|
2016-02-17 14:15:03 +08:00
|
|
|
offset = *(int64_t *)data;
|
|
|
|
addr = offset - size;
|
2016-02-22 12:56:19 +08:00
|
|
|
reltype = RL_REL;
|
2009-07-09 12:04:52 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
if (section != NO_SEG && section % 2) {
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "Mach-O format does not support"
|
2016-02-12 19:04:41 +08:00
|
|
|
" section base references");
|
|
|
|
} else if (wrt == NO_SEG) {
|
2016-02-22 12:56:19 +08:00
|
|
|
if (fmt.ptrsize == 8 &&
|
|
|
|
(s->flags & S_ATTR_SOME_INSTRUCTIONS)) {
|
|
|
|
uint8_t opcode[2];
|
|
|
|
|
|
|
|
opcode[0] = opcode[1] = 0;
|
|
|
|
|
|
|
|
/* HACK: Retrieve instruction opcode */
|
|
|
|
if (likely(s->data->datalen >= 2)) {
|
|
|
|
saa_fread(s->data, s->data->datalen-2, opcode, 2);
|
|
|
|
} else if (s->data->datalen == 1) {
|
|
|
|
saa_fread(s->data, 0, opcode+1, 1);
|
|
|
|
}
|
|
|
|
|
2016-02-23 16:13:49 +08:00
|
|
|
if ((opcode[0] != 0x0f && (opcode[1] & 0xfe) == 0xe8) ||
|
2016-02-22 12:56:19 +08:00
|
|
|
(opcode[0] == 0x0f && (opcode[1] & 0xf0) == 0x80)) {
|
|
|
|
/* Direct call, jmp, or jcc */
|
|
|
|
reltype = RL_BRANCH;
|
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
2016-02-22 12:56:19 +08:00
|
|
|
} else if (wrt == macho_gotpcrel_sect) {
|
|
|
|
reltype = RL_GOT;
|
|
|
|
|
|
|
|
if ((s->flags & S_ATTR_SOME_INSTRUCTIONS) &&
|
|
|
|
s->data->datalen >= 3) {
|
|
|
|
uint8_t gotload[3];
|
|
|
|
|
|
|
|
/* HACK: Retrieve instruction opcode */
|
|
|
|
saa_fread(s->data, s->data->datalen-3, gotload, 3);
|
|
|
|
if ((gotload[0] & 0xf8) == 0x48 &&
|
|
|
|
gotload[1] == 0x8b &&
|
|
|
|
(gotload[2] & 0307) == 0005) {
|
|
|
|
/* movq <reg>,[rel sym wrt ..gotpcrel] */
|
|
|
|
reltype = RL_GOTLOAD;
|
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
2018-10-09 09:49:56 +08:00
|
|
|
} else if (wrt == macho_tlvp_sect && fmt.ptrsize == 8) {
|
2016-02-22 12:56:19 +08:00
|
|
|
reltype = RL_TLV;
|
2016-02-12 19:04:41 +08:00
|
|
|
} else {
|
|
|
|
nasm_error(ERR_NONFATAL, "Mach-O format does not support"
|
|
|
|
" this use of WRT");
|
2016-02-22 12:56:19 +08:00
|
|
|
/* continue with RL_REL */
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-05-02 23:07:52 +08:00
|
|
|
addr += add_reloc(s, section, offset, reltype,
|
|
|
|
type == OUT_REL4ADR ? 4 : 8);
|
2015-11-05 05:43:29 +08:00
|
|
|
WRITELONG(p, addr);
|
2018-05-02 23:07:52 +08:00
|
|
|
sect_write(s, mydata, type == OUT_REL4ADR ? 4 : 8);
|
2005-09-23 05:41:17 +08:00
|
|
|
break;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
default:
|
2016-02-12 19:04:41 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "Unrepresentable relocation in Mach-O");
|
2005-09-23 05:41:17 +08:00
|
|
|
break;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 06:39:29 +08:00
|
|
|
#define S_CODE (S_REGULAR | S_ATTR_SOME_INSTRUCTIONS | S_ATTR_PURE_INSTRUCTIONS)
|
|
|
|
#define NO_TYPE S_NASM_TYPE_MASK
|
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
/* Translation table from traditional Unix section names to Mach-O */
|
2018-10-25 05:13:54 +08:00
|
|
|
static const struct macho_known_section {
|
2018-05-03 06:39:29 +08:00
|
|
|
const char *nasmsect;
|
|
|
|
const char *segname;
|
|
|
|
const char *sectname;
|
|
|
|
const uint32_t flags;
|
2018-10-25 05:13:54 +08:00
|
|
|
} known_sections[] = {
|
2018-05-03 06:39:29 +08:00
|
|
|
{ ".text", "__TEXT", "__text", S_CODE },
|
|
|
|
{ ".data", "__DATA", "__data", S_REGULAR },
|
|
|
|
{ ".rodata", "__DATA", "__const", S_REGULAR },
|
|
|
|
{ ".bss", "__DATA", "__bss", S_ZEROFILL },
|
|
|
|
{ ".debug_abbrev", "__DWARF", "__debug_abbrev", S_ATTR_DEBUG },
|
|
|
|
{ ".debug_info", "__DWARF", "__debug_info", S_ATTR_DEBUG },
|
|
|
|
{ ".debug_line", "__DWARF", "__debug_line", S_ATTR_DEBUG },
|
|
|
|
{ ".debug_str", "__DWARF", "__debug_str", S_ATTR_DEBUG },
|
2017-03-15 09:43:57 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Section type or attribute directives */
|
2018-05-05 17:54:34 +08:00
|
|
|
static const struct macho_known_section_attr {
|
2018-05-03 06:39:29 +08:00
|
|
|
const char *name;
|
|
|
|
uint32_t flags;
|
2017-03-15 09:43:57 +08:00
|
|
|
} sect_attribs[] = {
|
2018-05-03 06:39:29 +08:00
|
|
|
{ "data", S_REGULAR },
|
|
|
|
{ "code", S_CODE },
|
|
|
|
{ "mixed", S_REGULAR | S_ATTR_SOME_INSTRUCTIONS },
|
|
|
|
{ "bss", S_ZEROFILL },
|
|
|
|
{ "zerofill", S_ZEROFILL },
|
|
|
|
{ "no_dead_strip", NO_TYPE | S_ATTR_NO_DEAD_STRIP },
|
|
|
|
{ "live_support", NO_TYPE | S_ATTR_LIVE_SUPPORT },
|
|
|
|
{ "strip_static_syms", NO_TYPE | S_ATTR_STRIP_STATIC_SYMS },
|
|
|
|
{ "debug", NO_TYPE | S_ATTR_DEBUG },
|
2017-03-15 09:43:57 +08:00
|
|
|
{ NULL, 0 }
|
|
|
|
};
|
|
|
|
|
2018-10-25 05:13:54 +08:00
|
|
|
static const struct macho_known_section *
|
|
|
|
lookup_known_section(const char *name, bool by_sectname)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (name && name[0]) {
|
|
|
|
for (i = 0; i < ARRAY_SIZE(known_sections); i++) {
|
|
|
|
const char *p = by_sectname ?
|
|
|
|
known_sections[i].sectname :
|
|
|
|
known_sections[i].nasmsect;
|
|
|
|
if (!strcmp(name, p))
|
|
|
|
return &known_sections[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-12-19 03:12:46 +08:00
|
|
|
static int32_t macho_section(char *name, int *bits)
|
2005-09-23 05:22:33 +08:00
|
|
|
{
|
2018-10-25 05:13:54 +08:00
|
|
|
const struct macho_known_section *known_section;
|
2018-05-05 17:54:34 +08:00
|
|
|
const struct macho_known_section_attr *sa;
|
2007-04-14 00:47:53 +08:00
|
|
|
char *sectionAttributes;
|
2005-09-23 05:22:33 +08:00
|
|
|
struct section *s;
|
2016-02-17 04:39:26 +08:00
|
|
|
const char *section, *segment;
|
|
|
|
uint32_t flags;
|
|
|
|
char *currentAttribute;
|
|
|
|
char *comma;
|
2017-03-15 09:43:57 +08:00
|
|
|
|
2016-02-17 04:39:26 +08:00
|
|
|
bool new_seg;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-12 19:16:02 +08:00
|
|
|
/* Default to the appropriate number of bits. */
|
2005-12-09 08:27:26 +08:00
|
|
|
if (!name) {
|
2016-02-18 05:28:19 +08:00
|
|
|
*bits = fmt.ptrsize << 3;
|
2005-09-23 05:41:17 +08:00
|
|
|
name = ".text";
|
2005-12-09 08:27:26 +08:00
|
|
|
sectionAttributes = NULL;
|
|
|
|
} else {
|
|
|
|
sectionAttributes = name;
|
2007-08-26 13:48:54 +08:00
|
|
|
name = nasm_strsep(§ionAttributes, " \t");
|
2005-12-09 08:27:26 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-17 04:39:26 +08:00
|
|
|
section = segment = NULL;
|
|
|
|
flags = 0;
|
|
|
|
|
|
|
|
comma = strchr(name, ',');
|
|
|
|
if (comma) {
|
|
|
|
int len;
|
|
|
|
|
|
|
|
*comma = '\0';
|
|
|
|
segment = name;
|
|
|
|
section = comma+1;
|
|
|
|
|
|
|
|
len = strlen(segment);
|
|
|
|
if (len == 0) {
|
|
|
|
nasm_error(ERR_NONFATAL, "empty segment name\n");
|
2017-09-28 05:42:08 +08:00
|
|
|
} else if (len > 16) {
|
2016-02-17 04:39:26 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "segment name %s too long\n", segment);
|
|
|
|
}
|
|
|
|
|
|
|
|
len = strlen(section);
|
|
|
|
if (len == 0) {
|
|
|
|
nasm_error(ERR_NONFATAL, "empty section name\n");
|
2017-09-28 05:42:08 +08:00
|
|
|
} else if (len > 16) {
|
2016-02-17 04:39:26 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "section name %s too long\n", section);
|
|
|
|
}
|
|
|
|
|
2018-10-25 05:13:54 +08:00
|
|
|
known_section = lookup_known_section(section, true);
|
|
|
|
if (known_section)
|
|
|
|
flags = known_section->flags;
|
|
|
|
else
|
|
|
|
flags = S_REGULAR;
|
2016-02-17 04:39:26 +08:00
|
|
|
} else {
|
2018-10-25 05:13:54 +08:00
|
|
|
known_section = lookup_known_section(name, false);
|
|
|
|
if (!known_section) {
|
|
|
|
nasm_error(ERR_NONFATAL, "unknown section name %s\n", name);
|
|
|
|
return NO_SEG;
|
|
|
|
}
|
|
|
|
|
|
|
|
segment = known_section->segname;
|
|
|
|
section = known_section->sectname;
|
|
|
|
flags = known_section->flags;
|
2016-02-17 04:39:26 +08:00
|
|
|
}
|
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
/* try to find section with that name, or create it */
|
|
|
|
s = find_or_add_section(segment, section);
|
|
|
|
new_seg = is_new_section(s);
|
2016-02-17 04:39:26 +08:00
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
/* initialize it if it is a brand new section */
|
|
|
|
if (new_seg) {
|
|
|
|
*sectstail = s;
|
2016-02-17 04:39:26 +08:00
|
|
|
sectstail = &s->next;
|
|
|
|
|
|
|
|
s->data = saa_init(1L);
|
2016-02-17 09:38:58 +08:00
|
|
|
s->fileindex = ++seg_nsects;
|
2016-02-17 04:39:26 +08:00
|
|
|
s->align = -1;
|
|
|
|
s->pad = -1;
|
|
|
|
s->offset = -1;
|
2016-02-17 04:56:27 +08:00
|
|
|
s->by_name = false;
|
2016-02-17 04:39:26 +08:00
|
|
|
|
|
|
|
s->size = 0;
|
|
|
|
s->nreloc = 0;
|
|
|
|
s->flags = flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (comma)
|
|
|
|
*comma = ','; /* Restore comma */
|
|
|
|
|
2016-02-17 04:56:27 +08:00
|
|
|
s->by_name = s->by_name || comma; /* Was specified by name */
|
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
flags = NO_TYPE;
|
2016-02-17 04:39:26 +08:00
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
while (sectionAttributes &&
|
|
|
|
(currentAttribute = nasm_strsep(§ionAttributes, " \t"))) {
|
|
|
|
if (!*currentAttribute)
|
|
|
|
continue;
|
2016-02-17 04:39:26 +08:00
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
if (!nasm_strnicmp("align=", currentAttribute, 6)) {
|
|
|
|
char *end;
|
|
|
|
int newAlignment, value;
|
2016-02-17 04:39:26 +08:00
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
value = strtoul(currentAttribute + 6, (char**)&end, 0);
|
|
|
|
newAlignment = alignlog2_32(value);
|
2016-02-17 04:39:26 +08:00
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
if (0 != *end) {
|
2016-02-17 04:39:26 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
2017-03-15 09:43:57 +08:00
|
|
|
"unknown or missing alignment value \"%s\" "
|
|
|
|
"specified for section \"%s\"",
|
|
|
|
currentAttribute + 6,
|
|
|
|
name);
|
|
|
|
} else if (0 > newAlignment) {
|
|
|
|
nasm_error(ERR_NONFATAL,
|
|
|
|
"alignment of %d (for section \"%s\") is not "
|
|
|
|
"a power of two",
|
|
|
|
value,
|
2016-02-17 04:39:26 +08:00
|
|
|
name);
|
|
|
|
}
|
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
if (s->align < newAlignment)
|
|
|
|
s->align = newAlignment;
|
|
|
|
} else {
|
|
|
|
for (sa = sect_attribs; sa->name; sa++) {
|
|
|
|
if (!nasm_stricmp(sa->name, currentAttribute)) {
|
|
|
|
if ((sa->flags & S_NASM_TYPE_MASK) != NO_TYPE) {
|
|
|
|
flags = (flags & ~S_NASM_TYPE_MASK)
|
|
|
|
| (sa->flags & S_NASM_TYPE_MASK);
|
|
|
|
}
|
|
|
|
flags |= sa->flags & ~S_NASM_TYPE_MASK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sa->name) {
|
2016-02-17 04:39:26 +08:00
|
|
|
nasm_error(ERR_NONFATAL,
|
2017-03-15 09:43:57 +08:00
|
|
|
"unknown section attribute %s for section %s",
|
|
|
|
currentAttribute, name);
|
2016-02-17 04:39:26 +08:00
|
|
|
}
|
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
if ((flags & S_NASM_TYPE_MASK) != NO_TYPE) {
|
|
|
|
if (!new_seg && ((s->flags ^ flags) & S_NASM_TYPE_MASK)) {
|
|
|
|
nasm_error(ERR_NONFATAL,
|
|
|
|
"inconsistent section attributes for section %s\n",
|
|
|
|
name);
|
|
|
|
} else {
|
|
|
|
s->flags = (s->flags & ~S_NASM_TYPE_MASK) | flags;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s->flags |= flags & ~S_NASM_TYPE_MASK;
|
|
|
|
}
|
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
return s->subsection;
|
2018-05-31 05:43:46 +08:00
|
|
|
}
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
static int32_t macho_herelabel(const char *name, enum label_type type,
|
2018-06-28 11:20:21 +08:00
|
|
|
int32_t section, int32_t *subsection,
|
|
|
|
bool *copyoffset)
|
2018-05-31 05:43:46 +08:00
|
|
|
{
|
|
|
|
struct section *s;
|
2018-06-15 08:14:36 +08:00
|
|
|
int32_t subsec;
|
2018-06-02 09:02:54 +08:00
|
|
|
(void)name;
|
2018-06-15 08:14:36 +08:00
|
|
|
|
2018-05-31 05:43:46 +08:00
|
|
|
if (!(head_flags & MH_SUBSECTIONS_VIA_SYMBOLS))
|
|
|
|
return section;
|
|
|
|
|
2018-06-02 09:02:54 +08:00
|
|
|
/* No subsection only for local labels */
|
|
|
|
if (type == LBL_LOCAL)
|
2018-05-31 05:43:46 +08:00
|
|
|
return section;
|
2018-06-15 08:04:32 +08:00
|
|
|
|
2018-05-31 05:43:46 +08:00
|
|
|
s = get_section_by_index(section);
|
|
|
|
if (!s)
|
|
|
|
return section;
|
|
|
|
|
2018-06-15 08:14:36 +08:00
|
|
|
subsec = *subsection;
|
|
|
|
if (subsec == NO_SEG) {
|
|
|
|
/* Allocate a new subsection index */
|
|
|
|
subsec = *subsection = seg_alloc();
|
|
|
|
section_by_index = raa_write_ptr(section_by_index, subsec >> 1, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
s->subsection = subsec;
|
2018-06-28 11:20:21 +08:00
|
|
|
*copyoffset = true; /* Maintain previous offset */
|
2018-06-15 08:14:36 +08:00
|
|
|
return subsec;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2007-11-06 09:19:32 +08:00
|
|
|
static void macho_symdef(char *name, int32_t section, int64_t offset,
|
2007-04-14 00:47:53 +08:00
|
|
|
int is_global, char *special)
|
2005-09-23 05:22:33 +08:00
|
|
|
{
|
|
|
|
struct symbol *sym;
|
2017-06-23 14:36:34 +08:00
|
|
|
struct section *s;
|
2018-02-25 22:40:25 +08:00
|
|
|
bool special_used = false;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-02-25 22:40:25 +08:00
|
|
|
#if defined(DEBUG) && DEBUG>2
|
|
|
|
nasm_error(ERR_DEBUG,
|
2018-12-19 03:12:46 +08:00
|
|
|
" macho_symdef: %s, pass=%"PRId64" type %s, sec=%"PRIx32", off=%"PRIx64", is_global=%d, %s\n",
|
|
|
|
name, pass_count(), pass_types[pass_type()],
|
|
|
|
section, offset, is_global, special);
|
2018-02-25 22:40:25 +08:00
|
|
|
#endif
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
if (is_global == 3) {
|
2018-02-25 22:40:25 +08:00
|
|
|
if (special) {
|
|
|
|
int n = strcspn(special, " \t");
|
|
|
|
|
|
|
|
if (!nasm_strnicmp(special, "private_extern", n)) {
|
|
|
|
for (sym = syms; sym != NULL; sym = sym->next) {
|
|
|
|
if (!strcmp(name, sym->name)) {
|
|
|
|
if (sym->type & N_PEXT)
|
|
|
|
return; /* nothing to be done */
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "The Mach-O format does not "
|
2005-09-23 05:41:17 +08:00
|
|
|
"(yet) support forward reference fixups.");
|
|
|
|
return;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
|
|
|
|
/*
|
|
|
|
* This is a NASM special symbol. We never allow it into
|
|
|
|
* the Macho-O symbol table, even if it's a valid one. If it
|
|
|
|
* _isn't_ a valid one, we should barf immediately.
|
|
|
|
*/
|
|
|
|
if (strcmp(name, "..gotpcrel") && strcmp(name, "..tlvp"))
|
2009-07-19 09:07:17 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "unrecognized special symbol `%s'", name);
|
2016-02-17 14:15:03 +08:00
|
|
|
return;
|
2009-07-09 12:04:52 +08:00
|
|
|
}
|
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
sym = *symstail = nasm_zalloc(sizeof(struct symbol));
|
2005-10-08 06:30:56 +08:00
|
|
|
sym->next = NULL;
|
|
|
|
symstail = &sym->next;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
sym->name = name;
|
2005-09-23 05:22:33 +08:00
|
|
|
sym->strx = strslen;
|
|
|
|
sym->type = 0;
|
|
|
|
sym->desc = 0;
|
2017-06-23 14:36:34 +08:00
|
|
|
sym->symv[0].key = offset;
|
|
|
|
sym->symv[1].key = offset;
|
2005-10-08 06:30:56 +08:00
|
|
|
sym->initial_snum = -1;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* external and common symbols get N_EXT */
|
2009-07-09 12:04:52 +08:00
|
|
|
if (is_global != 0) {
|
2005-09-23 05:41:17 +08:00
|
|
|
sym->type |= N_EXT;
|
2016-02-17 09:38:58 +08:00
|
|
|
}
|
2018-02-25 22:40:25 +08:00
|
|
|
if (is_global == 1) {
|
|
|
|
/* check special to see if the global symbol shall be marked as private external: N_PEXT */
|
|
|
|
if (special) {
|
|
|
|
int n = strcspn(special, " \t");
|
|
|
|
|
|
|
|
if (!nasm_strnicmp(special, "private_extern", n))
|
|
|
|
sym->type |= N_PEXT;
|
|
|
|
else
|
|
|
|
nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'", n, special);
|
|
|
|
}
|
|
|
|
special_used = true;
|
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2018-10-09 09:49:49 +08:00
|
|
|
/* track the initially allocated symbol number for use in future fix-ups */
|
|
|
|
sym->initial_snum = nsyms;
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
if (section == NO_SEG) {
|
2005-09-23 05:41:17 +08:00
|
|
|
/* symbols in no section get absolute */
|
|
|
|
sym->type |= N_ABS;
|
|
|
|
sym->sect = NO_SECT;
|
2016-02-23 16:13:49 +08:00
|
|
|
|
2017-06-23 14:36:34 +08:00
|
|
|
s = &absolute_sect;
|
2005-09-23 05:22:33 +08:00
|
|
|
} else {
|
2017-06-23 14:36:34 +08:00
|
|
|
s = get_section_by_index(section);
|
2016-02-17 14:15:03 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
sym->type |= N_SECT;
|
|
|
|
|
|
|
|
/* get the in-file index of the section the symbol was defined in */
|
2016-02-17 14:15:03 +08:00
|
|
|
sym->sect = s ? s->fileindex : NO_SECT;
|
2016-02-12 19:04:41 +08:00
|
|
|
|
2016-02-17 14:15:03 +08:00
|
|
|
if (!s) {
|
2005-09-23 05:41:17 +08:00
|
|
|
/* remember symbol number of references to external
|
|
|
|
** symbols, this works because every external symbol gets
|
|
|
|
** its own section number allocated internally by nasm and
|
|
|
|
** can so be used as a key */
|
2016-02-17 09:38:58 +08:00
|
|
|
extsyms = raa_write(extsyms, section, nsyms);
|
2005-09-23 05:41:17 +08:00
|
|
|
|
|
|
|
switch (is_global) {
|
|
|
|
case 1:
|
|
|
|
case 2:
|
|
|
|
/* there isn't actually a difference between global
|
|
|
|
** and common symbols, both even have their size in
|
2017-06-23 14:36:34 +08:00
|
|
|
** sym->symv[0].key */
|
2005-09-23 05:41:17 +08:00
|
|
|
sym->type = N_EXT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* give an error on unfound section if it's not an
|
|
|
|
** external or common symbol (assemble_file() does a
|
|
|
|
** seg_alloc() on every call for them) */
|
2018-06-16 09:20:17 +08:00
|
|
|
nasm_panic("in-file index for section %d not found, is_global = %d", section, is_global);
|
2016-02-17 14:15:03 +08:00
|
|
|
break;
|
2005-09-23 05:41:17 +08:00
|
|
|
}
|
2016-02-17 14:15:03 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
2017-06-23 14:36:34 +08:00
|
|
|
|
|
|
|
if (s) {
|
|
|
|
s->syms[0] = rb_insert(s->syms[0], &sym->symv[0]);
|
|
|
|
if (is_global)
|
|
|
|
s->syms[1] = rb_insert(s->syms[1], &sym->symv[1]);
|
|
|
|
}
|
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
++nsyms;
|
2018-02-25 22:40:25 +08:00
|
|
|
|
|
|
|
if (special && !special_used)
|
|
|
|
nasm_error(ERR_NONFATAL, "no special symbol features supported here");
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
2010-04-29 03:17:13 +08:00
|
|
|
static void macho_sectalign(int32_t seg, unsigned int value)
|
2010-04-26 01:35:09 +08:00
|
|
|
{
|
|
|
|
struct section *s;
|
2016-02-12 19:23:25 +08:00
|
|
|
int align;
|
2010-04-26 01:35:09 +08:00
|
|
|
|
2016-02-17 09:38:58 +08:00
|
|
|
nasm_assert(!(seg & 1));
|
|
|
|
|
|
|
|
s = get_section_by_index(seg);
|
2010-04-26 01:35:09 +08:00
|
|
|
|
|
|
|
if (!s || !is_power2(value))
|
|
|
|
return;
|
|
|
|
|
2016-02-12 19:23:25 +08:00
|
|
|
align = alignlog2_32(value);
|
2016-02-13 02:50:38 +08:00
|
|
|
if (s->align < align)
|
2016-02-12 19:23:25 +08:00
|
|
|
s->align = align;
|
2010-04-26 01:35:09 +08:00
|
|
|
}
|
|
|
|
|
2008-06-21 06:20:16 +08:00
|
|
|
extern macros_t macho_stdmac[];
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
/* Comparison function for qsort symbol layout. */
|
|
|
|
static int layout_compare (const struct symbol **s1,
|
|
|
|
const struct symbol **s2)
|
|
|
|
{
|
|
|
|
return (strcmp ((*s1)->name, (*s2)->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The native assembler does a few things in a similar function
|
|
|
|
|
|
|
|
* Remove temporary labels
|
|
|
|
* Sort symbols according to local, external, undefined (by name)
|
|
|
|
* Order the string table
|
|
|
|
|
|
|
|
We do not remove temporary labels right now.
|
|
|
|
|
|
|
|
numsyms is the total number of symbols we have. strtabsize is the
|
|
|
|
number entries in the string table. */
|
|
|
|
|
2007-04-12 10:40:54 +08:00
|
|
|
static void macho_layout_symbols (uint32_t *numsyms,
|
|
|
|
uint32_t *strtabsize)
|
2005-10-08 06:30:56 +08:00
|
|
|
{
|
|
|
|
struct symbol *sym, **symp;
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t i,j;
|
2005-10-08 06:30:56 +08:00
|
|
|
|
|
|
|
*numsyms = 0;
|
2007-04-14 00:47:53 +08:00
|
|
|
*strtabsize = sizeof (char);
|
2005-10-08 06:30:56 +08:00
|
|
|
|
|
|
|
symp = &syms;
|
|
|
|
|
|
|
|
while ((sym = *symp)) {
|
|
|
|
/* Undefined symbols are now external. */
|
|
|
|
if (sym->type == N_UNDF)
|
|
|
|
sym->type |= N_EXT;
|
|
|
|
|
|
|
|
if ((sym->type & N_EXT) == 0) {
|
|
|
|
sym->snum = *numsyms;
|
|
|
|
*numsyms = *numsyms + 1;
|
|
|
|
nlocalsym++;
|
|
|
|
}
|
|
|
|
else {
|
2017-03-15 09:43:57 +08:00
|
|
|
if ((sym->type & N_TYPE) != N_UNDF) {
|
|
|
|
nextdefsym++;
|
2009-07-09 12:04:52 +08:00
|
|
|
} else {
|
2017-03-15 09:43:57 +08:00
|
|
|
nundefsym++;
|
|
|
|
}
|
2005-10-08 06:30:56 +08:00
|
|
|
|
|
|
|
/* If we handle debug info we'll want
|
|
|
|
to check for it here instead of just
|
|
|
|
adding the symbol to the string table. */
|
|
|
|
sym->strx = *strtabsize;
|
2007-04-12 10:40:54 +08:00
|
|
|
saa_wbytes (strs, sym->name, (int32_t)(strlen(sym->name) + 1));
|
2005-10-08 06:30:56 +08:00
|
|
|
*strtabsize += strlen(sym->name) + 1;
|
|
|
|
}
|
|
|
|
symp = &(sym->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, sort the symbols. Most of this code is a direct translation from
|
|
|
|
the Apple cctools symbol layout. We need to keep compatibility with that. */
|
|
|
|
/* Set the indexes for symbol groups into the symbol table */
|
|
|
|
ilocalsym = 0;
|
|
|
|
iextdefsym = nlocalsym;
|
|
|
|
iundefsym = nlocalsym + nextdefsym;
|
|
|
|
|
|
|
|
/* allocate arrays for sorting externals by name */
|
|
|
|
extdefsyms = nasm_malloc(nextdefsym * sizeof(struct symbol *));
|
|
|
|
undefsyms = nasm_malloc(nundefsym * sizeof(struct symbol *));
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
symp = &syms;
|
|
|
|
|
|
|
|
while ((sym = *symp)) {
|
|
|
|
|
|
|
|
if((sym->type & N_EXT) == 0) {
|
|
|
|
sym->strx = *strtabsize;
|
2007-04-12 10:40:54 +08:00
|
|
|
saa_wbytes (strs, sym->name, (int32_t)(strlen (sym->name) + 1));
|
2005-10-08 06:30:56 +08:00
|
|
|
*strtabsize += strlen(sym->name) + 1;
|
|
|
|
}
|
|
|
|
else {
|
2017-03-15 09:43:57 +08:00
|
|
|
if ((sym->type & N_TYPE) != N_UNDF) {
|
|
|
|
extdefsyms[i++] = sym;
|
2009-07-09 12:04:52 +08:00
|
|
|
} else {
|
2017-03-15 09:43:57 +08:00
|
|
|
undefsyms[j++] = sym;
|
|
|
|
}
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
symp = &(sym->next);
|
|
|
|
}
|
|
|
|
|
|
|
|
qsort(extdefsyms, nextdefsym, sizeof(struct symbol *),
|
|
|
|
(int (*)(const void *, const void *))layout_compare);
|
|
|
|
qsort(undefsyms, nundefsym, sizeof(struct symbol *),
|
|
|
|
(int (*)(const void *, const void *))layout_compare);
|
|
|
|
|
|
|
|
for(i = 0; i < nextdefsym; i++) {
|
|
|
|
extdefsyms[i]->snum = *numsyms;
|
|
|
|
*numsyms += 1;
|
|
|
|
}
|
|
|
|
for(j = 0; j < nundefsym; j++) {
|
|
|
|
undefsyms[j]->snum = *numsyms;
|
|
|
|
*numsyms += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
/* Calculate some values we'll need for writing later. */
|
|
|
|
|
|
|
|
static void macho_calculate_sizes (void)
|
2005-09-23 05:22:33 +08:00
|
|
|
{
|
2005-09-23 05:41:17 +08:00
|
|
|
struct section *s;
|
2016-02-12 09:51:37 +08:00
|
|
|
int fi;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* count sections and calculate in-memory and in-file offsets */
|
|
|
|
for (s = sects; s != NULL; s = s->next) {
|
2016-02-03 09:15:03 +08:00
|
|
|
uint64_t newaddr;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2008-12-09 16:18:43 +08:00
|
|
|
/* recalculate segment address based on alignment and vm size */
|
2016-02-12 17:23:53 +08:00
|
|
|
s->addr = seg_vmsize;
|
2016-02-03 09:15:03 +08:00
|
|
|
|
2008-12-09 16:18:43 +08:00
|
|
|
/* we need section alignment to calculate final section address */
|
|
|
|
if (s->align == -1)
|
|
|
|
s->align = DEFAULT_SECTION_ALIGNMENT;
|
|
|
|
|
2017-01-23 12:50:23 +08:00
|
|
|
newaddr = ALIGN(s->addr, UINT64_C(1) << s->align);
|
2016-02-03 09:15:03 +08:00
|
|
|
s->addr = newaddr;
|
|
|
|
|
2016-02-12 17:23:53 +08:00
|
|
|
seg_vmsize = newaddr + s->size;
|
2016-02-03 09:15:03 +08:00
|
|
|
|
|
|
|
/* zerofill sections aren't actually written to the file */
|
|
|
|
if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
|
|
|
|
/*
|
|
|
|
* LLVM/Xcode as always aligns the section data to 4
|
|
|
|
* bytes; there is a comment in the LLVM source code that
|
|
|
|
* perhaps aligning to pointer size would be better.
|
|
|
|
*/
|
2016-02-12 17:23:53 +08:00
|
|
|
s->pad = ALIGN(seg_filesize, 4) - seg_filesize;
|
|
|
|
s->offset = seg_filesize + s->pad;
|
|
|
|
seg_filesize += s->size + s->pad;
|
2017-04-08 16:57:11 +08:00
|
|
|
|
|
|
|
/* filesize and vmsize needs to be aligned */
|
|
|
|
seg_vmsize += s->pad;
|
2016-02-03 09:15:03 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate size of all headers, load commands and sections to
|
2005-09-27 06:13:42 +08:00
|
|
|
** get a pointer to the start of all the raw data */
|
2016-02-12 17:23:53 +08:00
|
|
|
if (seg_nsects > 0) {
|
|
|
|
++head_ncmds;
|
2016-02-18 05:28:19 +08:00
|
|
|
head_sizeofcmds += fmt.segcmd_size + seg_nsects * fmt.sectcmd_size;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nsyms > 0) {
|
2016-02-12 17:23:53 +08:00
|
|
|
++head_ncmds;
|
|
|
|
head_sizeofcmds += MACHO_SYMCMD_SIZE;
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
2016-02-12 09:51:37 +08:00
|
|
|
|
2016-02-17 03:42:13 +08:00
|
|
|
if (seg_nsects > MAX_SECT) {
|
2018-06-16 09:20:17 +08:00
|
|
|
nasm_fatal("MachO output is limited to %d sections\n",
|
2016-02-17 03:42:13 +08:00
|
|
|
MAX_SECT);
|
|
|
|
}
|
|
|
|
|
2016-02-12 09:51:37 +08:00
|
|
|
/* Create a table of sections by file index to avoid linear search */
|
2016-02-12 17:23:53 +08:00
|
|
|
sectstab = nasm_malloc((seg_nsects + 1) * sizeof(*sectstab));
|
2016-02-23 16:13:49 +08:00
|
|
|
sectstab[NO_SECT] = &absolute_sect;
|
2016-02-12 09:51:37 +08:00
|
|
|
for (s = sects, fi = 1; s != NULL; s = s->next, fi++)
|
|
|
|
sectstab[fi] = s;
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
/* Write out the header information for the file. */
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
static void macho_write_header (void)
|
|
|
|
{
|
2016-02-18 05:28:19 +08:00
|
|
|
fwriteint32_t(fmt.mh_magic, ofile); /* magic */
|
|
|
|
fwriteint32_t(fmt.cpu_type, ofile); /* CPU type */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(CPU_SUBTYPE_I386_ALL, ofile); /* CPU subtype */
|
|
|
|
fwriteint32_t(MH_OBJECT, ofile); /* Mach-O file type */
|
2016-02-12 17:23:53 +08:00
|
|
|
fwriteint32_t(head_ncmds, ofile); /* number of load commands */
|
|
|
|
fwriteint32_t(head_sizeofcmds, ofile); /* size of load commands */
|
2017-03-08 14:43:00 +08:00
|
|
|
fwriteint32_t(head_flags, ofile); /* flags, if any */
|
2016-02-18 05:28:19 +08:00
|
|
|
fwritezero(fmt.header_size - 7*4, ofile); /* reserved fields */
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
/* Write out the segment load command at offset. */
|
2005-09-23 05:41:17 +08:00
|
|
|
|
2009-07-09 12:04:52 +08:00
|
|
|
static uint32_t macho_write_segment (uint64_t offset)
|
2005-09-27 06:13:42 +08:00
|
|
|
{
|
2016-02-12 19:04:41 +08:00
|
|
|
uint64_t rel_base = alignptr(offset + seg_filesize);
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t s_reloff = 0;
|
2005-09-27 06:13:42 +08:00
|
|
|
struct section *s;
|
2005-09-23 05:41:17 +08:00
|
|
|
|
2016-02-18 05:28:19 +08:00
|
|
|
fwriteint32_t(fmt.lc_segment, ofile); /* cmd == LC_SEGMENT_64 */
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* size of load command including section load commands */
|
2016-02-18 05:28:19 +08:00
|
|
|
fwriteint32_t(fmt.segcmd_size + seg_nsects * fmt.sectcmd_size,
|
2016-02-12 09:51:37 +08:00
|
|
|
ofile);
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* in an MH_OBJECT file all sections are in one unnamed (name
|
|
|
|
** all zeros) segment */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwritezero(16, ofile);
|
2016-02-12 19:04:41 +08:00
|
|
|
fwriteptr(0, ofile); /* in-memory offset */
|
|
|
|
fwriteptr(seg_vmsize, ofile); /* in-memory size */
|
|
|
|
fwriteptr(offset, ofile); /* in-file offset to data */
|
|
|
|
fwriteptr(seg_filesize, ofile); /* in-file size */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(VM_PROT_DEFAULT, ofile); /* maximum vm protection */
|
|
|
|
fwriteint32_t(VM_PROT_DEFAULT, ofile); /* initial vm protection */
|
2016-02-12 17:23:53 +08:00
|
|
|
fwriteint32_t(seg_nsects, ofile); /* number of sections */
|
2016-02-12 19:04:41 +08:00
|
|
|
fwriteint32_t(0, ofile); /* no flags */
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* emit section headers */
|
|
|
|
for (s = sects; s != NULL; s = s->next) {
|
2016-02-17 10:04:39 +08:00
|
|
|
if (s->nreloc) {
|
|
|
|
nasm_assert((s->flags & SECTION_TYPE) != S_ZEROFILL);
|
|
|
|
s->flags |= S_ATTR_LOC_RELOC;
|
|
|
|
if (s->extreloc)
|
|
|
|
s->flags |= S_ATTR_EXT_RELOC;
|
2017-09-28 05:42:08 +08:00
|
|
|
} else if (!xstrncmp(s->segname, "__DATA") &&
|
|
|
|
!xstrncmp(s->sectname, "__const") &&
|
2016-02-17 10:04:39 +08:00
|
|
|
!s->by_name &&
|
|
|
|
!get_section_by_name("__TEXT", "__const")) {
|
|
|
|
/*
|
|
|
|
* The MachO equivalent to .rodata can be either
|
|
|
|
* __DATA,__const or __TEXT,__const; the latter only if
|
|
|
|
* there are no relocations. However, when mixed it is
|
|
|
|
* better to specify the segments explicitly.
|
|
|
|
*/
|
|
|
|
xstrncpy(s->segname, "__TEXT");
|
|
|
|
}
|
|
|
|
|
2014-10-22 03:50:47 +08:00
|
|
|
nasm_write(s->sectname, sizeof(s->sectname), ofile);
|
|
|
|
nasm_write(s->segname, sizeof(s->segname), ofile);
|
2016-02-12 19:04:41 +08:00
|
|
|
fwriteptr(s->addr, ofile);
|
|
|
|
fwriteptr(s->size, ofile);
|
2009-07-09 12:04:52 +08:00
|
|
|
|
|
|
|
/* dummy data for zerofill sections or proper values */
|
|
|
|
if ((s->flags & SECTION_TYPE) != S_ZEROFILL) {
|
2016-02-03 09:15:03 +08:00
|
|
|
nasm_assert(s->pad != (uint32_t)-1);
|
|
|
|
offset += s->pad;
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(offset, ofile);
|
2016-02-03 09:15:03 +08:00
|
|
|
offset += s->size;
|
2009-07-09 12:04:52 +08:00
|
|
|
/* Write out section alignment, as a power of two.
|
|
|
|
e.g. 32-bit word alignment would be 2 (2^2 = 4). */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(s->align, ofile);
|
2009-07-09 12:04:52 +08:00
|
|
|
/* To be compatible with cctools as we emit
|
|
|
|
a zero reloff if we have no relocations. */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(s->nreloc ? rel_base + s_reloff : 0, ofile);
|
|
|
|
fwriteint32_t(s->nreloc, ofile);
|
2009-07-09 12:04:52 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
s_reloff += s->nreloc * MACHO_RELINFO_SIZE;
|
2009-07-09 12:04:52 +08:00
|
|
|
} else {
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(0, ofile);
|
2016-02-12 19:04:41 +08:00
|
|
|
fwriteint32_t(s->align, ofile);
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(0, ofile);
|
|
|
|
fwriteint32_t(0, ofile);
|
2009-07-09 12:04:52 +08:00
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(s->flags, ofile); /* flags */
|
2016-02-03 09:15:03 +08:00
|
|
|
fwriteint32_t(0, ofile); /* reserved */
|
2016-02-12 19:04:41 +08:00
|
|
|
fwriteptr(0, ofile); /* reserved */
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
2005-09-23 05:41:17 +08:00
|
|
|
|
2016-02-12 17:23:53 +08:00
|
|
|
rel_padcnt = rel_base - offset;
|
2005-09-27 06:13:42 +08:00
|
|
|
offset = rel_base + s_reloff;
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* For a given chain of relocs r, write out the entire relocation
|
|
|
|
chain to the object file. */
|
|
|
|
|
|
|
|
static void macho_write_relocs (struct reloc *r)
|
|
|
|
{
|
|
|
|
while (r) {
|
2007-04-12 10:40:54 +08:00
|
|
|
uint32_t word2;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(r->addr, ofile); /* reloc offset */
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
word2 = r->snum;
|
|
|
|
word2 |= r->pcrel << 24;
|
|
|
|
word2 |= r->length << 25;
|
|
|
|
word2 |= r->ext << 27;
|
|
|
|
word2 |= r->type << 28;
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(word2, ofile); /* reloc data */
|
2005-09-27 06:13:42 +08:00
|
|
|
r = r->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write out the section data. */
|
|
|
|
static void macho_write_section (void)
|
|
|
|
{
|
2016-02-17 03:30:54 +08:00
|
|
|
struct section *s;
|
2005-09-27 06:13:42 +08:00
|
|
|
struct reloc *r;
|
2016-02-17 03:30:54 +08:00
|
|
|
uint8_t *p;
|
2016-02-12 19:04:41 +08:00
|
|
|
int32_t len;
|
2009-07-09 12:04:52 +08:00
|
|
|
int64_t l;
|
2016-02-12 19:04:41 +08:00
|
|
|
union offset {
|
|
|
|
uint64_t val;
|
|
|
|
uint8_t buf[8];
|
|
|
|
} blk;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
for (s = sects; s != NULL; s = s->next) {
|
|
|
|
if ((s->flags & SECTION_TYPE) == S_ZEROFILL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Like a.out Mach-O references things in the data or bss
|
|
|
|
* sections by addresses which are actually relative to the
|
|
|
|
* start of the _text_ section, in the _file_. See outaout.c
|
|
|
|
* for more information. */
|
|
|
|
saa_rewind(s->data);
|
|
|
|
for (r = s->relocs; r != NULL; r = r->next) {
|
2016-02-12 19:04:41 +08:00
|
|
|
len = (uint32_t)1 << r->length;
|
|
|
|
if (len > 4) /* Can this ever be an issue?! */
|
|
|
|
len = 8;
|
|
|
|
blk.val = 0;
|
|
|
|
saa_fread(s->data, r->addr, blk.buf, len);
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* get offset based on relocation type */
|
2016-02-12 19:04:41 +08:00
|
|
|
#ifdef WORDS_LITTLEENDIAN
|
|
|
|
l = blk.val;
|
|
|
|
#else
|
|
|
|
l = blk.buf[0];
|
|
|
|
l += ((int64_t)blk.buf[1]) << 8;
|
|
|
|
l += ((int64_t)blk.buf[2]) << 16;
|
|
|
|
l += ((int64_t)blk.buf[3]) << 24;
|
|
|
|
l += ((int64_t)blk.buf[4]) << 32;
|
|
|
|
l += ((int64_t)blk.buf[5]) << 40;
|
|
|
|
l += ((int64_t)blk.buf[6]) << 48;
|
|
|
|
l += ((int64_t)blk.buf[7]) << 56;
|
|
|
|
#endif
|
|
|
|
|
2005-12-06 07:17:17 +08:00
|
|
|
/* If the relocation is internal add to the current section
|
|
|
|
offset. Otherwise the only value we need is the symbol
|
|
|
|
offset which we already have. The linker takes care
|
|
|
|
of the rest of the address. */
|
|
|
|
if (!r->ext) {
|
2016-02-16 16:29:48 +08:00
|
|
|
/* generate final address by section address and offset */
|
|
|
|
nasm_assert(r->snum <= seg_nsects);
|
|
|
|
l += sectstab[r->snum]->addr;
|
2016-02-23 16:13:49 +08:00
|
|
|
if (r->pcrel)
|
|
|
|
l -= s->addr;
|
2016-05-10 02:36:55 +08:00
|
|
|
} else if (r->pcrel && r->type == GENERIC_RELOC_VANILLA) {
|
2016-05-10 02:16:10 +08:00
|
|
|
l -= s->addr;
|
2005-12-06 07:17:17 +08:00
|
|
|
}
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* write new offset back */
|
2016-02-12 19:04:41 +08:00
|
|
|
p = blk.buf;
|
|
|
|
WRITEDLONG(p, l);
|
|
|
|
saa_fwrite(s->data, r->addr, blk.buf, len);
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dump the section data to file */
|
2016-02-03 09:15:03 +08:00
|
|
|
fwritezero(s->pad, ofile);
|
2009-07-19 09:07:17 +08:00
|
|
|
saa_fpwrite(s->data, ofile);
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
/* pad last section up to reloc entries on pointer boundary */
|
2016-02-12 17:23:53 +08:00
|
|
|
fwritezero(rel_padcnt, ofile);
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* emit relocation entries */
|
|
|
|
for (s = sects; s != NULL; s = s->next)
|
|
|
|
macho_write_relocs (s->relocs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write out the symbol table. We should already have sorted this
|
|
|
|
before now. */
|
|
|
|
static void macho_write_symtab (void)
|
|
|
|
{
|
|
|
|
struct symbol *sym;
|
2009-07-09 12:04:52 +08:00
|
|
|
uint64_t i;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* we don't need to pad here since MACHO_RELINFO_SIZE == 8 */
|
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
for (sym = syms; sym != NULL; sym = sym->next) {
|
|
|
|
if ((sym->type & N_EXT) == 0) {
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(sym->strx, ofile); /* string table entry number */
|
2014-10-22 03:50:47 +08:00
|
|
|
nasm_write(&sym->type, 1, ofile); /* symbol type */
|
|
|
|
nasm_write(&sym->sect, 1, ofile); /* section */
|
|
|
|
fwriteint16_t(sym->desc, ofile); /* description */
|
2005-10-08 06:30:56 +08:00
|
|
|
|
|
|
|
/* Fix up the symbol value now that we know the final section
|
|
|
|
sizes. */
|
|
|
|
if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
|
2016-02-12 17:23:53 +08:00
|
|
|
nasm_assert(sym->sect <= seg_nsects);
|
2017-06-23 14:36:34 +08:00
|
|
|
sym->symv[0].key += sectstab[sym->sect]->addr;
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 14:36:34 +08:00
|
|
|
fwriteptr(sym->symv[0].key, ofile); /* value (i.e. offset) */
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
}
|
2005-09-27 06:13:42 +08:00
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
for (i = 0; i < nextdefsym; i++) {
|
|
|
|
sym = extdefsyms[i];
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(sym->strx, ofile);
|
2014-10-22 03:50:47 +08:00
|
|
|
nasm_write(&sym->type, 1, ofile); /* symbol type */
|
|
|
|
nasm_write(&sym->sect, 1, ofile); /* section */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint16_t(sym->desc, ofile); /* description */
|
2005-09-27 06:13:42 +08:00
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
/* Fix up the symbol value now that we know the final section
|
|
|
|
sizes. */
|
2005-09-27 06:13:42 +08:00
|
|
|
if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
|
2016-02-12 17:23:53 +08:00
|
|
|
nasm_assert(sym->sect <= seg_nsects);
|
2017-06-23 14:36:34 +08:00
|
|
|
sym->symv[0].key += sectstab[sym->sect]->addr;
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 14:36:34 +08:00
|
|
|
fwriteptr(sym->symv[0].key, ofile); /* value (i.e. offset) */
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < nundefsym; i++) {
|
|
|
|
sym = undefsyms[i];
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(sym->strx, ofile);
|
2014-10-22 03:50:47 +08:00
|
|
|
nasm_write(&sym->type, 1, ofile); /* symbol type */
|
|
|
|
nasm_write(&sym->sect, 1, ofile); /* section */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint16_t(sym->desc, ofile); /* description */
|
2005-10-08 06:30:56 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
/* Fix up the symbol value now that we know the final section
|
|
|
|
sizes. */
|
2005-10-08 06:30:56 +08:00
|
|
|
if (((sym->type & N_TYPE) == N_SECT) && (sym->sect != NO_SECT)) {
|
2016-02-12 17:23:53 +08:00
|
|
|
nasm_assert(sym->sect <= seg_nsects);
|
2017-06-23 14:36:34 +08:00
|
|
|
sym->symv[0].key += sectstab[sym->sect]->addr;
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 14:36:34 +08:00
|
|
|
fwriteptr(sym->symv[0].key, ofile); /* value (i.e. offset) */
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
2009-07-09 12:04:52 +08:00
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fixup the snum in the relocation entries, we should be
|
2009-07-09 12:04:52 +08:00
|
|
|
doing this only for externally referenced symbols. */
|
2005-10-08 06:30:56 +08:00
|
|
|
static void macho_fixup_relocs (struct reloc *r)
|
|
|
|
{
|
|
|
|
struct symbol *sym;
|
|
|
|
|
|
|
|
while (r != NULL) {
|
|
|
|
if (r->ext) {
|
2016-02-12 19:04:41 +08:00
|
|
|
for (sym = syms; sym != NULL; sym = sym->next) {
|
2005-10-08 06:30:56 +08:00
|
|
|
if (sym->initial_snum == r->snum) {
|
2016-02-12 19:04:41 +08:00
|
|
|
r->snum = sym->snum;
|
|
|
|
break;
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
|
|
|
r = r->next;
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write out the object file. */
|
|
|
|
|
|
|
|
static void macho_write (void)
|
|
|
|
{
|
2009-07-09 12:04:52 +08:00
|
|
|
uint64_t offset = 0;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* mach-o object file structure:
|
|
|
|
**
|
|
|
|
** mach header
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t magic
|
2005-09-27 06:13:42 +08:00
|
|
|
** int cpu type
|
|
|
|
** int cpu subtype
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t mach file type
|
|
|
|
** uint32_t number of load commands
|
|
|
|
** uint32_t size of all load commands
|
2005-09-27 06:13:42 +08:00
|
|
|
** (includes section struct size of segment command)
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t flags
|
2005-09-27 06:13:42 +08:00
|
|
|
**
|
|
|
|
** segment command
|
2016-02-12 19:04:41 +08:00
|
|
|
** uint32_t command type == LC_SEGMENT[_64]
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t size of load command
|
2005-09-27 06:13:42 +08:00
|
|
|
** (including section load commands)
|
2007-04-14 00:47:53 +08:00
|
|
|
** char[16] segment name
|
2016-02-12 19:04:41 +08:00
|
|
|
** pointer in-memory offset
|
|
|
|
** pointer in-memory size
|
|
|
|
** pointer in-file offset to data area
|
|
|
|
** pointer in-file size
|
2005-09-27 06:13:42 +08:00
|
|
|
** (in-memory size excluding zerofill sections)
|
|
|
|
** int maximum vm protection
|
|
|
|
** int initial vm protection
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t number of sections
|
|
|
|
** uint32_t flags
|
2005-09-27 06:13:42 +08:00
|
|
|
**
|
|
|
|
** section commands
|
2007-04-14 00:47:53 +08:00
|
|
|
** char[16] section name
|
|
|
|
** char[16] segment name
|
2016-02-12 19:04:41 +08:00
|
|
|
** pointer in-memory offset
|
|
|
|
** pointer in-memory size
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t in-file offset
|
|
|
|
** uint32_t alignment
|
2005-09-27 06:13:42 +08:00
|
|
|
** (irrelevant in MH_OBJECT)
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t in-file offset of relocation entires
|
|
|
|
** uint32_t number of relocations
|
|
|
|
** uint32_t flags
|
|
|
|
** uint32_t reserved
|
|
|
|
** uint32_t reserved
|
2005-09-27 06:13:42 +08:00
|
|
|
**
|
|
|
|
** symbol table command
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint32_t command type == LC_SYMTAB
|
|
|
|
** uint32_t size of load command
|
|
|
|
** uint32_t symbol table offset
|
|
|
|
** uint32_t number of symbol table entries
|
|
|
|
** uint32_t string table offset
|
|
|
|
** uint32_t string table size
|
2005-09-27 06:13:42 +08:00
|
|
|
**
|
|
|
|
** raw section data
|
|
|
|
**
|
2016-02-12 19:04:41 +08:00
|
|
|
** padding to pointer boundary
|
2005-09-27 06:13:42 +08:00
|
|
|
**
|
|
|
|
** relocation data (struct reloc)
|
2007-04-12 10:40:54 +08:00
|
|
|
** int32_t offset
|
2005-09-27 06:13:42 +08:00
|
|
|
** uint data (symbolnum, pcrel, length, extern, type)
|
|
|
|
**
|
|
|
|
** symbol table data (struct nlist)
|
2007-04-12 10:40:54 +08:00
|
|
|
** int32_t string table entry number
|
|
|
|
** uint8_t type
|
2005-09-27 06:13:42 +08:00
|
|
|
** (extern, absolute, defined in section)
|
2007-04-12 10:40:54 +08:00
|
|
|
** uint8_t section
|
2005-09-27 06:13:42 +08:00
|
|
|
** (0 for global symbols, section number of definition (>= 1, <=
|
|
|
|
** 254) for local symbols, size of variable for common symbols
|
|
|
|
** [type == extern])
|
2007-04-12 10:40:54 +08:00
|
|
|
** int16_t description
|
2005-09-27 06:13:42 +08:00
|
|
|
** (for stab debugging format)
|
2016-02-12 19:04:41 +08:00
|
|
|
** pointer value (i.e. file offset) of symbol or stab offset
|
2005-09-27 06:13:42 +08:00
|
|
|
**
|
|
|
|
** string table data
|
|
|
|
** list of null-terminated strings
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Emit the Mach-O header. */
|
|
|
|
macho_write_header();
|
|
|
|
|
2016-02-18 05:28:19 +08:00
|
|
|
offset = fmt.header_size + head_sizeofcmds;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
|
|
|
/* emit the segment load command */
|
2016-02-12 17:23:53 +08:00
|
|
|
if (seg_nsects > 0)
|
2005-09-27 06:13:42 +08:00
|
|
|
offset = macho_write_segment (offset);
|
|
|
|
else
|
2019-08-07 10:18:36 +08:00
|
|
|
nasm_warn(WARN_OTHER, "no sections?");
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
if (nsyms > 0) {
|
2005-09-23 05:41:17 +08:00
|
|
|
/* write out symbol command */
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(LC_SYMTAB, ofile); /* cmd == LC_SYMTAB */
|
|
|
|
fwriteint32_t(MACHO_SYMCMD_SIZE, ofile); /* size of load command */
|
|
|
|
fwriteint32_t(offset, ofile); /* symbol table offset */
|
|
|
|
fwriteint32_t(nsyms, ofile); /* number of symbol
|
2005-09-23 05:41:17 +08:00
|
|
|
** table entries */
|
2016-02-18 05:28:19 +08:00
|
|
|
offset += nsyms * fmt.nlist_size;
|
2009-07-19 09:07:17 +08:00
|
|
|
fwriteint32_t(offset, ofile); /* string table offset */
|
|
|
|
fwriteint32_t(strslen, ofile); /* string table size */
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* emit section data */
|
2016-02-12 17:23:53 +08:00
|
|
|
if (seg_nsects > 0)
|
2005-09-27 06:13:42 +08:00
|
|
|
macho_write_section ();
|
2005-09-23 05:41:17 +08:00
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
/* emit symbol table if we have symbols */
|
|
|
|
if (nsyms > 0)
|
|
|
|
macho_write_symtab ();
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
/* we don't need to pad here, we are already aligned */
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* emit string table */
|
2009-07-19 09:07:17 +08:00
|
|
|
saa_fpwrite(strs, ofile);
|
2005-09-27 06:13:42 +08:00
|
|
|
}
|
|
|
|
/* We do quite a bit here, starting with finalizing all of the data
|
|
|
|
for the object file, writing, and then freeing all of the data from
|
|
|
|
the file. */
|
|
|
|
|
2016-03-08 14:53:06 +08:00
|
|
|
static void macho_cleanup(void)
|
2005-09-27 06:13:42 +08:00
|
|
|
{
|
|
|
|
struct section *s;
|
|
|
|
struct reloc *r;
|
2005-10-08 06:30:56 +08:00
|
|
|
struct symbol *sym;
|
2005-09-27 06:13:42 +08:00
|
|
|
|
2017-04-08 17:25:14 +08:00
|
|
|
dfmt->cleanup();
|
|
|
|
|
2005-10-08 06:30:56 +08:00
|
|
|
/* Sort all symbols. */
|
|
|
|
macho_layout_symbols (&nsyms, &strslen);
|
|
|
|
|
|
|
|
/* Fixup relocation entries */
|
|
|
|
for (s = sects; s != NULL; s = s->next) {
|
|
|
|
macho_fixup_relocs (s->relocs);
|
|
|
|
}
|
|
|
|
|
2005-09-27 06:13:42 +08:00
|
|
|
/* First calculate and finalize needed values. */
|
|
|
|
macho_calculate_sizes();
|
|
|
|
macho_write();
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
/* free up everything */
|
|
|
|
while (sects->next) {
|
2005-09-23 05:41:17 +08:00
|
|
|
s = sects;
|
|
|
|
sects = sects->next;
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
saa_free(s->data);
|
|
|
|
while (s->relocs != NULL) {
|
|
|
|
r = s->relocs;
|
|
|
|
s->relocs = s->relocs->next;
|
|
|
|
nasm_free(r);
|
|
|
|
}
|
2005-09-23 05:22:33 +08:00
|
|
|
|
2005-09-23 05:41:17 +08:00
|
|
|
nasm_free(s);
|
2005-09-23 05:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
saa_free(strs);
|
2018-05-31 05:43:46 +08:00
|
|
|
|
2005-09-23 05:22:33 +08:00
|
|
|
raa_free(extsyms);
|
2005-10-08 06:30:56 +08:00
|
|
|
|
2016-02-12 12:20:10 +08:00
|
|
|
while (syms) {
|
2007-11-13 14:11:46 +08:00
|
|
|
sym = syms;
|
|
|
|
syms = syms->next;
|
|
|
|
nasm_free (sym);
|
2005-10-08 06:30:56 +08:00
|
|
|
}
|
2016-02-12 12:20:10 +08:00
|
|
|
|
|
|
|
nasm_free(extdefsyms);
|
|
|
|
nasm_free(undefsyms);
|
|
|
|
nasm_free(sectstab);
|
2018-06-15 08:14:36 +08:00
|
|
|
raa_free(section_by_index);
|
|
|
|
hash_free(§ion_by_name);
|
2007-11-13 14:11:46 +08:00
|
|
|
}
|
2005-10-08 06:30:56 +08:00
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
static bool macho_set_section_attribute_by_symbol(const char *label, uint32_t flags)
|
|
|
|
{
|
|
|
|
struct section *s;
|
|
|
|
int32_t nasm_seg;
|
|
|
|
int64_t offset;
|
|
|
|
|
2019-09-13 08:26:23 +08:00
|
|
|
if (lookup_label(label, &nasm_seg, &offset) == LBL_none) {
|
2017-03-15 09:43:57 +08:00
|
|
|
nasm_error(ERR_NONFATAL, "unknown symbol `%s' in no_dead_strip", label);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = get_section_by_index(nasm_seg);
|
|
|
|
if (!s) {
|
|
|
|
nasm_error(ERR_NONFATAL, "symbol `%s' is external or absolute", label);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s->flags |= flags;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Mark a symbol for no dead stripping
|
|
|
|
*/
|
|
|
|
static enum directive_result macho_no_dead_strip(const char *labels)
|
|
|
|
{
|
|
|
|
char *s, *p, *ep;
|
|
|
|
char ec;
|
|
|
|
enum directive_result rv = DIRR_ERROR;
|
|
|
|
|
|
|
|
p = s = nasm_strdup(labels);
|
|
|
|
while (*p) {
|
|
|
|
ep = nasm_skip_identifier(p);
|
|
|
|
if (!ep) {
|
|
|
|
nasm_error(ERR_NONFATAL, "invalid symbol in NO_DEAD_STRIP");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
ec = *ep;
|
|
|
|
if (ec && ec != ',' && !nasm_isspace(ec)) {
|
|
|
|
nasm_error(ERR_NONFATAL, "cannot parse contents after symbol");
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
*ep = '\0';
|
2018-12-19 03:12:46 +08:00
|
|
|
if (!pass_first()) {
|
2017-03-15 09:43:57 +08:00
|
|
|
if (!macho_set_section_attribute_by_symbol(p, S_ATTR_NO_DEAD_STRIP))
|
|
|
|
rv = DIRR_ERROR;
|
|
|
|
}
|
|
|
|
*ep = ec;
|
|
|
|
p = nasm_skip_spaces(ep);
|
|
|
|
if (*p == ',')
|
|
|
|
p = nasm_skip_spaces(++p);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = DIRR_OK;
|
|
|
|
|
|
|
|
err:
|
|
|
|
nasm_free(s);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-08 14:43:00 +08:00
|
|
|
/*
|
2017-03-15 09:43:57 +08:00
|
|
|
* Mach-O pragmas
|
2017-03-08 14:43:00 +08:00
|
|
|
*/
|
|
|
|
static enum directive_result
|
2017-03-15 09:43:57 +08:00
|
|
|
macho_pragma(const struct pragma *pragma)
|
2017-03-08 14:43:00 +08:00
|
|
|
{
|
2017-03-15 09:43:57 +08:00
|
|
|
switch (pragma->opcode) {
|
|
|
|
case D_SUBSECTIONS_VIA_SYMBOLS:
|
|
|
|
if (*pragma->tail)
|
|
|
|
return DIRR_BADPARAM;
|
|
|
|
|
2018-12-19 03:12:46 +08:00
|
|
|
if (!pass_first())
|
2017-03-15 09:43:57 +08:00
|
|
|
head_flags |= MH_SUBSECTIONS_VIA_SYMBOLS;
|
|
|
|
|
2018-08-14 04:50:18 +08:00
|
|
|
/* Jmp-match optimization conflicts */
|
|
|
|
optimizing.flag |= OPTIM_DISABLE_JMP_MATCH;
|
|
|
|
|
2017-03-15 09:43:57 +08:00
|
|
|
return DIRR_OK;
|
|
|
|
|
|
|
|
case D_NO_DEAD_STRIP:
|
|
|
|
return macho_no_dead_strip(pragma->tail);
|
|
|
|
|
|
|
|
default:
|
|
|
|
return DIRR_UNKNOWN; /* Not a Mach-O directive */
|
|
|
|
}
|
2017-03-08 14:43:00 +08:00
|
|
|
}
|
2017-03-15 09:43:57 +08:00
|
|
|
|
|
|
|
static const struct pragma_facility macho_pragma_list[] = {
|
|
|
|
{ "macho", macho_pragma },
|
|
|
|
{ NULL, macho_pragma } /* Implements macho32/macho64 namespaces */
|
|
|
|
};
|
|
|
|
|
2017-04-08 17:37:11 +08:00
|
|
|
static void macho_dbg_generate(void)
|
|
|
|
{
|
|
|
|
uint8_t *p_buf = NULL, *p_buf_base = NULL;
|
2017-04-08 17:45:54 +08:00
|
|
|
size_t saa_len = 0, high_addr = 0, total_len = 0;
|
2017-04-08 17:37:11 +08:00
|
|
|
struct section *p_section = NULL;
|
|
|
|
/* calculated at debug_str and referenced at debug_info */
|
2017-04-25 12:08:23 +08:00
|
|
|
uint32_t producer_str_offset = 0, module_str_offset = 0, dir_str_offset = 0;
|
2017-04-08 17:37:11 +08:00
|
|
|
|
|
|
|
/* debug section defines */
|
|
|
|
{
|
|
|
|
int bits = 0;
|
2018-12-19 03:12:46 +08:00
|
|
|
macho_section(".debug_abbrev", &bits);
|
|
|
|
macho_section(".debug_info", &bits);
|
|
|
|
macho_section(".debug_line", &bits);
|
|
|
|
macho_section(".debug_str", &bits);
|
2017-04-08 17:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dw section walk to find high_addr and total_len */
|
|
|
|
{
|
2017-04-24 14:46:41 +08:00
|
|
|
struct dw_sect_list *p_sect;
|
|
|
|
|
|
|
|
list_for_each(p_sect, dw_head_sect) {
|
2017-04-08 17:37:11 +08:00
|
|
|
uint64_t offset = get_section_by_index(p_sect->section)->size;
|
2017-04-08 17:45:54 +08:00
|
|
|
struct SAA *p_linep = p_sect->psaa;
|
|
|
|
|
|
|
|
saa_write8(p_linep, 2); /* std op 2 */
|
|
|
|
saa_write8(p_linep, offset - p_sect->offset);
|
|
|
|
saa_write8(p_linep, DW_LNS_extended_op);
|
|
|
|
saa_write8(p_linep, 1); /* operand length */
|
|
|
|
saa_write8(p_linep, DW_LNE_end_sequence);
|
|
|
|
|
|
|
|
total_len += p_linep->datalen;
|
2017-04-08 17:37:11 +08:00
|
|
|
high_addr += offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:45:54 +08:00
|
|
|
/* debug line */
|
|
|
|
{
|
2017-04-24 14:46:41 +08:00
|
|
|
struct dw_sect_list *p_sect;
|
|
|
|
size_t linep_off, buf_size;
|
2017-04-08 17:45:54 +08:00
|
|
|
struct SAA *p_lines = saa_init(1L);
|
2017-04-27 03:31:41 +08:00
|
|
|
struct dir_list *p_dir;
|
|
|
|
struct file_list *p_file;
|
2017-04-08 17:45:54 +08:00
|
|
|
|
|
|
|
p_section = get_section_by_name("__DWARF", "__debug_line");
|
|
|
|
nasm_assert(p_section != NULL);
|
|
|
|
|
|
|
|
saa_write8(p_lines, 1); /* minimum instruction length */
|
|
|
|
saa_write8(p_lines, 1); /* initial value of "is_stmt" */
|
|
|
|
saa_write8(p_lines, DW_LN_BASE); /* line base */
|
|
|
|
saa_write8(p_lines, DW_LN_RANGE); /* line range */
|
|
|
|
saa_write8(p_lines, DW_OPCODE_BASE); /* opcode base */
|
|
|
|
saa_write8(p_lines, 0); /* std opcode 1 length */
|
|
|
|
saa_write8(p_lines, 1); /* std opcode 2 length */
|
|
|
|
saa_write8(p_lines, 1); /* std opcode 3 length */
|
|
|
|
saa_write8(p_lines, 1); /* std opcode 4 length */
|
|
|
|
saa_write8(p_lines, 1); /* std opcode 5 length */
|
|
|
|
saa_write8(p_lines, 0); /* std opcode 6 length */
|
|
|
|
saa_write8(p_lines, 0); /* std opcode 7 length */
|
|
|
|
saa_write8(p_lines, 0); /* std opcode 8 length */
|
|
|
|
saa_write8(p_lines, 1); /* std opcode 9 length */
|
|
|
|
saa_write8(p_lines, 0); /* std opcode 10 length */
|
|
|
|
saa_write8(p_lines, 0); /* std opcode 11 length */
|
|
|
|
saa_write8(p_lines, 1); /* std opcode 12 length */
|
2017-04-27 03:31:41 +08:00
|
|
|
list_for_each(p_dir, dw_head_dir) {
|
|
|
|
saa_wcstring(p_lines, p_dir->dir_name);
|
|
|
|
}
|
2017-04-08 17:45:54 +08:00
|
|
|
saa_write8(p_lines, 0); /* end of table */
|
|
|
|
|
2017-04-27 03:31:41 +08:00
|
|
|
list_for_each(p_file, dw_head_file) {
|
|
|
|
saa_wcstring(p_lines, p_file->file_name);
|
|
|
|
saa_write8(p_lines, p_file->dir->dir); /* directory id */
|
|
|
|
saa_write8(p_lines, 0); /* time */
|
|
|
|
saa_write8(p_lines, 0); /* size */
|
|
|
|
}
|
2017-04-08 17:45:54 +08:00
|
|
|
saa_write8(p_lines, 0); /* end of table */
|
|
|
|
|
|
|
|
linep_off = p_lines->datalen;
|
|
|
|
/* 10 bytes for initial & prolong length, and dwarf version info */
|
|
|
|
buf_size = saa_len = linep_off + total_len + 10;
|
|
|
|
p_buf_base = p_buf = nasm_malloc(buf_size);
|
|
|
|
|
|
|
|
WRITELONG(p_buf, saa_len - 4); /* initial length; size excluding itself */
|
|
|
|
WRITESHORT(p_buf, 2); /* dwarf version */
|
|
|
|
WRITELONG(p_buf, linep_off); /* prolong length */
|
|
|
|
|
|
|
|
saa_rnbytes(p_lines, p_buf, linep_off);
|
|
|
|
p_buf += linep_off;
|
|
|
|
saa_free(p_lines);
|
|
|
|
|
2017-04-24 14:46:41 +08:00
|
|
|
list_for_each(p_sect, dw_head_sect) {
|
2017-04-08 17:45:54 +08:00
|
|
|
struct SAA *p_linep = p_sect->psaa;
|
2017-04-25 12:08:23 +08:00
|
|
|
|
2017-04-08 17:45:54 +08:00
|
|
|
saa_len = p_linep->datalen;
|
|
|
|
saa_rnbytes(p_linep, p_buf, saa_len);
|
|
|
|
p_buf += saa_len;
|
2017-04-25 12:08:23 +08:00
|
|
|
|
2017-04-08 17:45:54 +08:00
|
|
|
saa_free(p_linep);
|
|
|
|
}
|
|
|
|
|
|
|
|
macho_output(p_section->index, p_buf_base, OUT_RAWDATA, buf_size, NO_SEG, 0);
|
|
|
|
|
2017-04-25 12:08:23 +08:00
|
|
|
nasm_free(p_buf_base);
|
2017-04-08 17:45:54 +08:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:37:11 +08:00
|
|
|
/* string section */
|
|
|
|
{
|
|
|
|
struct SAA *p_str = saa_init(1L);
|
2017-04-25 12:08:23 +08:00
|
|
|
char *cur_path = nasm_realpath(module_name);
|
|
|
|
char *cur_file = nasm_basename(cur_path);
|
|
|
|
char *cur_dir = nasm_dirname(cur_path);
|
2017-04-08 17:37:11 +08:00
|
|
|
|
|
|
|
p_section = get_section_by_name("__DWARF", "__debug_str");
|
|
|
|
nasm_assert(p_section != NULL);
|
|
|
|
|
|
|
|
producer_str_offset = 0;
|
2018-11-24 21:33:18 +08:00
|
|
|
module_str_offset = dir_str_offset = saa_wcstring(p_str, nasm_signature());
|
2017-04-25 12:08:23 +08:00
|
|
|
dir_str_offset += saa_wcstring(p_str, cur_file);
|
|
|
|
saa_wcstring(p_str, cur_dir);
|
2017-04-08 17:37:11 +08:00
|
|
|
|
|
|
|
saa_len = p_str->datalen;
|
|
|
|
p_buf = nasm_malloc(saa_len);
|
|
|
|
saa_rnbytes(p_str, p_buf, saa_len);
|
|
|
|
macho_output(p_section->index, p_buf, OUT_RAWDATA, saa_len, NO_SEG, 0);
|
2017-04-22 21:40:43 +08:00
|
|
|
|
2017-04-25 12:08:23 +08:00
|
|
|
nasm_free(cur_path);
|
|
|
|
nasm_free(cur_file);
|
|
|
|
nasm_free(cur_dir);
|
|
|
|
saa_free(p_str);
|
|
|
|
nasm_free(p_buf);
|
2017-04-08 17:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* debug info */
|
|
|
|
{
|
|
|
|
struct SAA *p_info = saa_init(1L);
|
|
|
|
|
|
|
|
p_section = get_section_by_name("__DWARF", "__debug_info");
|
|
|
|
nasm_assert(p_section != NULL);
|
|
|
|
|
|
|
|
/* size will be overwritten once determined, so skip in p_info layout */
|
|
|
|
saa_write16(p_info, 2); /* dwarf version */
|
|
|
|
saa_write32(p_info, 0); /* offset info abbrev */
|
|
|
|
saa_write8(p_info, (ofmt == &of_macho64) ? 8 : 4); /* pointer size */
|
|
|
|
|
|
|
|
saa_write8(p_info, 1); /* abbrev entry number */
|
|
|
|
|
|
|
|
saa_write32(p_info, producer_str_offset); /* offset from string table for DW_AT_producer */
|
|
|
|
saa_write16(p_info, DW_LANG_Mips_Assembler); /* DW_AT_language */
|
|
|
|
saa_write32(p_info, module_str_offset); /* offset from string table for DW_AT_name */
|
2017-04-25 12:08:23 +08:00
|
|
|
saa_write32(p_info, dir_str_offset); /* offset from string table for DW_AT_comp_dir */
|
2017-04-08 17:37:11 +08:00
|
|
|
saa_write32(p_info, 0); /* DW_AT_stmt_list */
|
|
|
|
|
|
|
|
if (ofmt == &of_macho64) {
|
|
|
|
saa_write64(p_info, 0); /* DW_AT_low_pc */
|
|
|
|
saa_write64(p_info, high_addr); /* DW_AT_high_pc */
|
|
|
|
} else {
|
|
|
|
saa_write32(p_info, 0); /* DW_AT_low_pc */
|
|
|
|
saa_write32(p_info, high_addr); /* DW_AT_high_pc */
|
|
|
|
}
|
|
|
|
|
|
|
|
saa_write8(p_info, 2); /* abbrev entry number */
|
|
|
|
|
|
|
|
if (ofmt == &of_macho64) {
|
|
|
|
saa_write64(p_info, 0); /* DW_AT_low_pc */
|
|
|
|
saa_write64(p_info, 0); /* DW_AT_frame_base */
|
|
|
|
} else {
|
|
|
|
saa_write32(p_info, 0); /* DW_AT_low_pc */
|
|
|
|
saa_write32(p_info, 0); /* DW_AT_frame_base */
|
|
|
|
}
|
|
|
|
saa_write8(p_info, DW_END_default);
|
|
|
|
|
|
|
|
saa_len = p_info->datalen;
|
|
|
|
p_buf_base = p_buf = nasm_malloc(saa_len + 4); /* 4B for size info */
|
|
|
|
|
|
|
|
WRITELONG(p_buf, saa_len);
|
|
|
|
saa_rnbytes(p_info, p_buf, saa_len);
|
|
|
|
macho_output(p_section->index, p_buf_base, OUT_RAWDATA, saa_len + 4, NO_SEG, 0);
|
|
|
|
|
|
|
|
saa_free(p_info);
|
2017-04-25 12:08:23 +08:00
|
|
|
nasm_free(p_buf_base);
|
2017-04-08 17:37:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* abbrev section */
|
|
|
|
{
|
|
|
|
struct SAA *p_abbrev = saa_init(1L);
|
|
|
|
|
|
|
|
p_section = get_section_by_name("__DWARF", "__debug_abbrev");
|
|
|
|
nasm_assert(p_section != NULL);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, 1); /* entry number */
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_TAG_compile_unit);
|
|
|
|
saa_write8(p_abbrev, DW_CHILDREN_yes);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_producer);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_strp);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_language);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_data2);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_name);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_strp);
|
2017-04-22 21:40:43 +08:00
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_comp_dir);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_strp);
|
2017-04-08 17:37:11 +08:00
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_stmt_list);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_data4);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_low_pc);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_addr);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_high_pc);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_addr);
|
|
|
|
|
|
|
|
saa_write16(p_abbrev, DW_END_default);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, 2); /* entry number */
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_TAG_subprogram);
|
|
|
|
saa_write8(p_abbrev, DW_CHILDREN_no);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_low_pc);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_addr);
|
|
|
|
|
|
|
|
saa_write8(p_abbrev, DW_AT_frame_base);
|
|
|
|
saa_write8(p_abbrev, DW_FORM_addr);
|
|
|
|
|
|
|
|
saa_write16(p_abbrev, DW_END_default);
|
|
|
|
|
2017-09-28 06:05:03 +08:00
|
|
|
saa_write8(p_abbrev, 0); /* Terminal zero entry */
|
|
|
|
|
2017-04-08 17:37:11 +08:00
|
|
|
saa_len = p_abbrev->datalen;
|
|
|
|
|
|
|
|
p_buf = nasm_malloc(saa_len);
|
|
|
|
|
|
|
|
saa_rnbytes(p_abbrev, p_buf, saa_len);
|
|
|
|
macho_output(p_section->index, p_buf, OUT_RAWDATA, saa_len, NO_SEG, 0);
|
|
|
|
|
|
|
|
saa_free(p_abbrev);
|
2017-04-25 12:08:23 +08:00
|
|
|
nasm_free(p_buf);
|
2017-04-08 17:37:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-27 03:13:08 +08:00
|
|
|
static void new_file_list (const char *file_name, const char *dir_name)
|
|
|
|
{
|
|
|
|
struct dir_list *dir_list;
|
|
|
|
bool need_new_dir_list = true;
|
|
|
|
|
|
|
|
nasm_new(dw_cur_file);
|
|
|
|
dw_cur_file->file = ++dw_num_files;
|
|
|
|
dw_cur_file->file_name = file_name;
|
|
|
|
if(!dw_head_file) {
|
|
|
|
dw_head_file = dw_cur_file;
|
|
|
|
} else {
|
|
|
|
*dw_last_file_next = dw_cur_file;
|
|
|
|
}
|
|
|
|
dw_last_file_next = &(dw_cur_file->next);
|
|
|
|
|
|
|
|
if(dw_head_dir) {
|
|
|
|
list_for_each(dir_list, dw_head_dir) {
|
|
|
|
if(!(strcmp(dir_name, dir_list->dir_name))) {
|
|
|
|
dw_cur_file->dir = dir_list;
|
|
|
|
need_new_dir_list = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(need_new_dir_list)
|
|
|
|
{
|
|
|
|
nasm_new(dir_list);
|
|
|
|
dir_list->dir = dw_num_dirs++;
|
|
|
|
dir_list->dir_name = dir_name;
|
|
|
|
if(!dw_head_dir) {
|
|
|
|
dw_head_dir = dir_list;
|
|
|
|
} else {
|
|
|
|
*dw_last_dir_next = dir_list;
|
|
|
|
}
|
|
|
|
dw_last_dir_next = &(dir_list->next);
|
|
|
|
dw_cur_file->dir = dir_list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:22:05 +08:00
|
|
|
static void macho_dbg_init(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static void macho_dbg_linenum(const char *file_name, int32_t line_num, int32_t segto)
|
|
|
|
{
|
2017-04-08 17:25:14 +08:00
|
|
|
bool need_new_list = true;
|
2017-04-27 03:13:08 +08:00
|
|
|
const char *cur_file = nasm_basename(file_name);
|
|
|
|
const char *cur_dir = nasm_dirname(file_name);
|
2017-04-08 17:22:05 +08:00
|
|
|
(void)segto;
|
2017-04-08 17:25:14 +08:00
|
|
|
|
2017-04-27 03:13:08 +08:00
|
|
|
if(!dw_cur_file || strcmp(cur_file, dw_cur_file->file_name) ||
|
|
|
|
strcmp(cur_dir, dw_cur_file->dir->dir_name)) {
|
|
|
|
if(dw_head_file) {
|
2017-04-24 14:46:41 +08:00
|
|
|
struct file_list *match;
|
2017-04-08 17:25:14 +08:00
|
|
|
|
2017-04-27 03:13:08 +08:00
|
|
|
list_for_each(match, dw_head_file) {
|
|
|
|
if(!(strcmp(cur_file, match->file_name)) &&
|
|
|
|
!(strcmp(cur_dir, match->dir->dir_name))) {
|
|
|
|
dw_cur_file = match;
|
|
|
|
dw_cur_file->dir = match->dir;
|
2017-04-08 17:25:14 +08:00
|
|
|
need_new_list = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(need_new_list) {
|
2017-04-27 03:13:08 +08:00
|
|
|
new_file_list(cur_file, cur_dir);
|
2017-04-08 17:25:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg_immcall = true;
|
|
|
|
cur_line = line_num;
|
2017-04-08 17:22:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void macho_dbg_output(int type, void *param)
|
|
|
|
{
|
2017-04-08 17:25:14 +08:00
|
|
|
struct section_info *sinfo_param = (struct section_info *)param;
|
|
|
|
int32_t secto = sinfo_param->secto;
|
|
|
|
bool need_new_sect = false;
|
2017-04-08 17:45:54 +08:00
|
|
|
struct SAA *p_linep = NULL;
|
2017-04-08 17:22:05 +08:00
|
|
|
(void)type;
|
2017-04-08 17:25:14 +08:00
|
|
|
|
|
|
|
if(!(dw_cur_sect && (dw_cur_sect->section == secto))) {
|
|
|
|
need_new_sect = true;
|
|
|
|
if(dw_head_sect) {
|
|
|
|
struct dw_sect_list *match = dw_head_sect;
|
|
|
|
uint32_t idx = 0;
|
|
|
|
|
|
|
|
for(; idx < dw_num_sects; idx++) {
|
|
|
|
if(match->section == secto) {
|
|
|
|
dw_cur_sect = match;
|
|
|
|
need_new_sect = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
match = match->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(need_new_sect) {
|
|
|
|
nasm_new(dw_cur_sect);
|
|
|
|
dw_num_sects ++;
|
2017-04-08 17:45:54 +08:00
|
|
|
p_linep = dw_cur_sect->psaa = saa_init(1L);
|
2017-04-08 17:25:14 +08:00
|
|
|
dw_cur_sect->line = dw_cur_sect->file = 1;
|
|
|
|
dw_cur_sect->offset = 0;
|
|
|
|
dw_cur_sect->next = NULL;
|
|
|
|
dw_cur_sect->section = secto;
|
|
|
|
|
2017-04-08 17:45:54 +08:00
|
|
|
saa_write8(p_linep, DW_LNS_extended_op);
|
|
|
|
saa_write8(p_linep, (ofmt == &of_macho64) ? 9 : 5);
|
|
|
|
saa_write8(p_linep, DW_LNE_set_address);
|
|
|
|
if (ofmt == &of_macho64) {
|
|
|
|
saa_write64(p_linep, 0);
|
|
|
|
} else {
|
|
|
|
saa_write32(p_linep, 0);
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:25:14 +08:00
|
|
|
if(!dw_head_sect) {
|
|
|
|
dw_head_sect = dw_last_sect = dw_cur_sect;
|
|
|
|
} else {
|
|
|
|
dw_last_sect->next = dw_cur_sect;
|
|
|
|
dw_last_sect = dw_cur_sect;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(dbg_immcall == true) {
|
|
|
|
int32_t line_delta = cur_line - dw_cur_sect->line;
|
2017-04-08 17:45:54 +08:00
|
|
|
int32_t offset_delta = sinfo_param->size - dw_cur_sect->offset;
|
2017-04-27 03:13:08 +08:00
|
|
|
uint32_t cur_file = dw_cur_file->file;
|
2017-04-08 17:45:54 +08:00
|
|
|
p_linep = dw_cur_sect->psaa;
|
2017-04-08 17:25:14 +08:00
|
|
|
|
|
|
|
if(cur_file != dw_cur_sect->file) {
|
2017-04-08 17:45:54 +08:00
|
|
|
saa_write8(p_linep, DW_LNS_set_file);
|
|
|
|
saa_write8(p_linep, cur_file);
|
2017-04-08 17:25:14 +08:00
|
|
|
dw_cur_sect->file = cur_file;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(line_delta) {
|
2017-04-08 17:45:54 +08:00
|
|
|
int special_opcode = (line_delta - DW_LN_BASE) + (DW_LN_RANGE * offset_delta) +
|
|
|
|
DW_OPCODE_BASE;
|
|
|
|
|
|
|
|
if((line_delta >= DW_LN_BASE) && (line_delta < DW_MAX_LN) &&
|
|
|
|
(special_opcode < DW_MAX_SP_OPCODE)) {
|
|
|
|
saa_write8(p_linep, special_opcode);
|
|
|
|
} else {
|
|
|
|
saa_write8(p_linep, DW_LNS_advance_line);
|
|
|
|
saa_wleb128s(p_linep, line_delta);
|
|
|
|
if(offset_delta) {
|
|
|
|
saa_write8(p_linep, DW_LNS_advance_pc);
|
|
|
|
saa_wleb128u(p_linep, offset_delta);
|
|
|
|
}
|
|
|
|
saa_write8(p_linep, DW_LNS_copy);
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:25:14 +08:00
|
|
|
dw_cur_sect->line = cur_line;
|
|
|
|
dw_cur_sect->offset = sinfo_param->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbg_immcall = false;
|
|
|
|
}
|
2017-04-08 17:22:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void macho_dbg_cleanup(void)
|
|
|
|
{
|
2017-04-08 17:37:11 +08:00
|
|
|
/* dwarf sectors generation */
|
|
|
|
macho_dbg_generate();
|
|
|
|
|
2017-04-08 17:25:14 +08:00
|
|
|
{
|
|
|
|
struct dw_sect_list *p_sect = dw_head_sect;
|
2017-04-27 03:13:08 +08:00
|
|
|
struct file_list *p_file = dw_head_file;
|
2017-04-08 17:25:14 +08:00
|
|
|
uint32_t idx = 0;
|
|
|
|
|
|
|
|
for(; idx < dw_num_sects; idx++) {
|
|
|
|
struct dw_sect_list *next = p_sect->next;
|
|
|
|
nasm_free(p_sect);
|
|
|
|
p_sect = next;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(idx = 0; idx < dw_num_files; idx++) {
|
|
|
|
struct file_list *next = p_file->next;
|
|
|
|
nasm_free(p_file);
|
|
|
|
p_file = next;
|
|
|
|
}
|
|
|
|
}
|
2017-04-08 17:22:05 +08:00
|
|
|
}
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
#ifdef OF_MACHO32
|
2016-02-12 19:13:00 +08:00
|
|
|
static const struct macho_fmt macho32_fmt = {
|
|
|
|
4,
|
|
|
|
MH_MAGIC,
|
|
|
|
CPU_TYPE_I386,
|
|
|
|
LC_SEGMENT,
|
|
|
|
MACHO_HEADER_SIZE,
|
|
|
|
MACHO_SEGCMD_SIZE,
|
|
|
|
MACHO_SECTCMD_SIZE,
|
2016-02-17 14:15:03 +08:00
|
|
|
MACHO_NLIST_SIZE,
|
|
|
|
RL_MAX_32,
|
|
|
|
GENERIC_RELOC_VANILLA,
|
|
|
|
GENERIC_RELOC_VANILLA,
|
2017-06-23 14:36:34 +08:00
|
|
|
GENERIC_RELOC_TLV,
|
|
|
|
false /* Allow segment-relative relocations */
|
2016-02-12 19:13:00 +08:00
|
|
|
};
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
static void macho32_init(void)
|
|
|
|
{
|
2016-02-18 05:28:19 +08:00
|
|
|
fmt = macho32_fmt;
|
2016-02-12 19:04:41 +08:00
|
|
|
macho_init();
|
|
|
|
|
|
|
|
macho_gotpcrel_sect = NO_SEG;
|
|
|
|
}
|
|
|
|
|
2017-04-08 17:22:05 +08:00
|
|
|
static const struct dfmt macho32_df_dwarf = {
|
2019-08-10 17:28:17 +08:00
|
|
|
"Mach-O i386 dwarf for Darwin/MacOS",
|
2017-04-08 17:22:05 +08:00
|
|
|
"dwarf",
|
|
|
|
macho_dbg_init,
|
|
|
|
macho_dbg_linenum,
|
|
|
|
null_debug_deflabel,
|
|
|
|
null_debug_directive,
|
|
|
|
null_debug_typevalue,
|
|
|
|
macho_dbg_output,
|
|
|
|
macho_dbg_cleanup,
|
|
|
|
NULL /*pragma list*/
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dfmt * const macho32_df_arr[2] =
|
|
|
|
{ &macho32_df_dwarf, NULL };
|
|
|
|
|
2016-02-18 12:59:22 +08:00
|
|
|
const struct ofmt of_macho32 = {
|
2019-08-10 17:28:17 +08:00
|
|
|
"Mach-O i386 (Mach, including MacOS X and variants)",
|
2016-02-13 05:36:37 +08:00
|
|
|
"macho32",
|
2017-12-21 05:33:49 +08:00
|
|
|
".o",
|
2018-06-02 09:14:16 +08:00
|
|
|
0,
|
2016-02-12 19:04:41 +08:00
|
|
|
32,
|
2017-04-08 17:22:05 +08:00
|
|
|
macho32_df_arr,
|
|
|
|
&macho32_df_dwarf,
|
2016-02-12 19:04:41 +08:00
|
|
|
macho_stdmac,
|
|
|
|
macho32_init,
|
2018-06-15 08:14:36 +08:00
|
|
|
null_reset,
|
2016-09-25 00:46:47 +08:00
|
|
|
nasm_do_legacy_output,
|
2016-02-12 19:04:41 +08:00
|
|
|
macho_output,
|
|
|
|
macho_symdef,
|
|
|
|
macho_section,
|
2018-05-31 05:43:46 +08:00
|
|
|
macho_herelabel,
|
2016-02-12 19:04:41 +08:00
|
|
|
macho_sectalign,
|
2018-06-15 13:41:00 +08:00
|
|
|
null_segbase,
|
2017-03-15 09:43:57 +08:00
|
|
|
null_directive,
|
2017-03-08 13:32:37 +08:00
|
|
|
macho_cleanup,
|
2017-06-23 14:36:34 +08:00
|
|
|
macho_pragma_list
|
2016-02-12 19:04:41 +08:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OF_MACHO64
|
2016-02-12 19:13:00 +08:00
|
|
|
static const struct macho_fmt macho64_fmt = {
|
|
|
|
8,
|
|
|
|
MH_MAGIC_64,
|
|
|
|
CPU_TYPE_X86_64,
|
|
|
|
LC_SEGMENT_64,
|
|
|
|
MACHO_HEADER64_SIZE,
|
|
|
|
MACHO_SEGCMD64_SIZE,
|
|
|
|
MACHO_SECTCMD64_SIZE,
|
2016-02-17 14:15:03 +08:00
|
|
|
MACHO_NLIST64_SIZE,
|
|
|
|
RL_MAX_64,
|
|
|
|
X86_64_RELOC_UNSIGNED,
|
|
|
|
X86_64_RELOC_SIGNED,
|
2017-06-23 14:36:34 +08:00
|
|
|
X86_64_RELOC_TLV,
|
|
|
|
true /* Force symbol-relative relocations */
|
2016-02-12 19:13:00 +08:00
|
|
|
};
|
|
|
|
|
2016-02-12 19:04:41 +08:00
|
|
|
static void macho64_init(void)
|
|
|
|
{
|
2016-02-18 05:28:19 +08:00
|
|
|
fmt = macho64_fmt;
|
2016-02-12 19:04:41 +08:00
|
|
|
macho_init();
|
|
|
|
|
|
|
|
/* add special symbol for ..gotpcrel */
|
2016-02-17 14:15:03 +08:00
|
|
|
macho_gotpcrel_sect = seg_alloc() + 1;
|
2018-06-02 09:02:54 +08:00
|
|
|
backend_label("..gotpcrel", macho_gotpcrel_sect, 0L);
|
2016-02-12 19:04:41 +08:00
|
|
|
}
|
|
|
|
|
2017-04-08 17:22:05 +08:00
|
|
|
static const struct dfmt macho64_df_dwarf = {
|
2019-08-10 17:28:17 +08:00
|
|
|
"Mach-O x86-64 dwarf for Darwin/MacOS",
|
2017-04-08 17:22:05 +08:00
|
|
|
"dwarf",
|
|
|
|
macho_dbg_init,
|
|
|
|
macho_dbg_linenum,
|
|
|
|
null_debug_deflabel,
|
|
|
|
null_debug_directive,
|
|
|
|
null_debug_typevalue,
|
|
|
|
macho_dbg_output,
|
|
|
|
macho_dbg_cleanup,
|
|
|
|
NULL /*pragma list*/
|
|
|
|
};
|
|
|
|
|
|
|
|
static const struct dfmt * const macho64_df_arr[2] =
|
|
|
|
{ &macho64_df_dwarf, NULL };
|
|
|
|
|
2016-02-18 12:59:22 +08:00
|
|
|
const struct ofmt of_macho64 = {
|
2019-08-10 17:28:17 +08:00
|
|
|
"Mach-O x86-64 (Mach, including MacOS X and variants)",
|
2009-07-09 12:04:52 +08:00
|
|
|
"macho64",
|
2017-12-21 05:33:49 +08:00
|
|
|
".o",
|
2018-06-02 09:14:16 +08:00
|
|
|
0,
|
2016-02-12 12:40:07 +08:00
|
|
|
64,
|
2017-04-08 17:22:05 +08:00
|
|
|
macho64_df_arr,
|
|
|
|
&macho64_df_dwarf,
|
2005-09-23 05:22:33 +08:00
|
|
|
macho_stdmac,
|
2016-02-12 19:04:41 +08:00
|
|
|
macho64_init,
|
2018-06-15 08:14:36 +08:00
|
|
|
null_reset,
|
2016-09-25 00:46:47 +08:00
|
|
|
nasm_do_legacy_output,
|
2005-09-23 05:22:33 +08:00
|
|
|
macho_output,
|
|
|
|
macho_symdef,
|
|
|
|
macho_section,
|
2018-05-31 05:43:46 +08:00
|
|
|
macho_herelabel,
|
2010-04-29 03:17:13 +08:00
|
|
|
macho_sectalign,
|
2018-06-15 13:41:00 +08:00
|
|
|
null_segbase,
|
2017-03-15 09:43:57 +08:00
|
|
|
null_directive,
|
2017-03-08 13:32:37 +08:00
|
|
|
macho_cleanup,
|
2017-03-15 09:43:57 +08:00
|
|
|
macho_pragma_list,
|
2005-09-23 05:22:33 +08:00
|
|
|
};
|
2016-02-12 19:04:41 +08:00
|
|
|
#endif
|
2005-09-23 05:22:33 +08:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* mode:c
|
|
|
|
* c-basic-offset:4
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* end of file */
|