nasm/output/outaout.c

956 lines
30 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2009 The NASM Authors - All Rights Reserved
* 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.
*
* 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.
2002-05-01 04:51:32 +08:00
*
* ----------------------------------------------------------------------- */
/*
* outaout.c output routines for the Netwide Assembler to produce
* Linux a.out object files
2002-05-01 04:51:32 +08:00
*/
#include "compiler.h"
2002-05-01 04:51:32 +08:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
2002-05-01 04:51:32 +08:00
#include "nasm.h"
#include "nasmlib.h"
#include "saa.h"
#include "raa.h"
#include "stdscan.h"
#include "eval.h"
#include "output/outform.h"
#include "output/outlib.h"
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
#if defined OF_AOUT || defined OF_AOUTB
#define RELTYPE_ABSOLUTE 0x00
#define RELTYPE_RELATIVE 0x01
#define RELTYPE_GOTPC 0x01 /* no explicit GOTPC in a.out */
#define RELTYPE_GOTOFF 0x10
#define RELTYPE_GOT 0x10 /* distinct from GOTOFF bcos sym not sect */
#define RELTYPE_PLT 0x21
#define RELTYPE_SYMFLAG 0x08
2002-05-01 04:51:32 +08:00
struct Reloc {
struct Reloc *next;
int32_t address; /* relative to _start_ of section */
int32_t symbol; /* symbol number or -ve section id */
2005-01-16 06:15:51 +08:00
int bytes; /* 2 or 4 */
int reltype; /* see above */
2002-05-01 04:51:32 +08:00
};
struct Symbol {
int32_t strpos; /* string table position of name */
2005-01-16 06:15:51 +08:00
int type; /* symbol type - see flags below */
int32_t value; /* address, or COMMON variable size */
int32_t size; /* size for data or function exports */
int32_t segment; /* back-reference used by gsym_reloc */
2005-01-16 06:15:51 +08:00
struct Symbol *next; /* list of globals in each section */
struct Symbol *nextfwd; /* list of unresolved-size symbols */
char *name; /* for unresolved-size symbols */
int32_t symnum; /* index into symbol table */
2002-05-01 04:51:32 +08:00
};
/*
* Section IDs - used in Reloc.symbol when negative, and in
* Symbol.type when positive.
*/
2005-01-16 06:15:51 +08:00
#define SECT_ABS 2 /* absolute value */
#define SECT_TEXT 4 /* text section */
#define SECT_DATA 6 /* data section */
#define SECT_BSS 8 /* bss section */
#define SECT_MASK 0xE /* mask out any of the above */
2002-05-01 04:51:32 +08:00
/*
2002-05-01 04:52:49 +08:00
* More flags used in Symbol.type.
2002-05-01 04:51:32 +08:00
*/
2005-01-16 06:15:51 +08:00
#define SYM_GLOBAL 1 /* it's a global symbol */
#define SYM_DATA 0x100 /* used for shared libs */
#define SYM_FUNCTION 0x200 /* used for shared libs */
#define SYM_WITH_SIZE 0x4000 /* not output; internal only */
2002-05-01 04:51:32 +08:00
/*
* Bit more explanation of symbol types: SECT_xxx denotes a local
* symbol. SECT_xxx|SYM_GLOBAL denotes a global symbol, defined in
* this module. Just SYM_GLOBAL, with zero value, denotes an
* external symbol referenced in this module. And just SYM_GLOBAL,
* but with a non-zero value, declares a C `common' variable, of
* size `value'.
*/
struct Section {
struct SAA *data;
uint32_t len, size, nrelocs;
int32_t index;
2002-05-01 04:51:32 +08:00
struct Reloc *head, **tail;
2002-05-01 04:52:49 +08:00
struct Symbol *gsyms, *asym;
2002-05-01 04:51:32 +08:00
};
2002-05-01 04:52:49 +08:00
static struct Section stext, sdata, sbss;
2002-05-01 04:51:32 +08:00
static struct SAA *syms;
static uint32_t nsyms;
2002-05-01 04:51:32 +08:00
static struct RAA *bsym;
static struct SAA *strs;
static uint32_t strslen;
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
static struct Symbol *fwds;
static int bsd;
static int is_pic;
2002-05-01 04:51:32 +08:00
static void aout_write(void);
static void aout_write_relocs(struct Reloc *);
static void aout_write_syms(void);
static void aout_sect_write(struct Section *, const uint8_t *,
uint32_t);
2002-05-01 04:51:32 +08:00
static void aout_pad_sections(void);
static void aout_fixup_relocs(struct Section *);
2002-05-01 04:52:49 +08:00
/*
* Special section numbers which are used to define special
* symbols, which can be used with WRT to provide PIC relocation
* types.
*/
static int32_t aout_gotpc_sect, aout_gotoff_sect;
static int32_t aout_got_sect, aout_plt_sect;
static int32_t aout_sym_sect;
2002-05-01 04:52:49 +08:00
static void aoutg_init(void)
2002-05-01 04:53:55 +08:00
{
2005-01-16 06:15:51 +08:00
stext.data = saa_init(1L);
stext.head = NULL;
stext.tail = &stext.head;
sdata.data = saa_init(1L);
sdata.head = NULL;
sdata.tail = &sdata.head;
2002-05-01 04:52:49 +08:00
stext.len = stext.size = sdata.len = sdata.size = sbss.len = 0;
2002-05-01 04:51:32 +08:00
stext.nrelocs = sdata.nrelocs = 0;
2002-05-01 04:52:49 +08:00
stext.gsyms = sdata.gsyms = sbss.gsyms = NULL;
2002-05-01 04:51:32 +08:00
stext.index = seg_alloc();
sdata.index = seg_alloc();
2002-05-01 04:52:49 +08:00
sbss.index = seg_alloc();
stext.asym = sdata.asym = sbss.asym = NULL;
syms = saa_init((int32_t)sizeof(struct Symbol));
2002-05-01 04:51:32 +08:00
nsyms = 0;
bsym = raa_init();
strs = saa_init(1L);
strslen = 0;
2002-05-01 04:52:49 +08:00
fwds = NULL;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:49 +08:00
#ifdef OF_AOUT
static void aout_init(void)
2002-05-01 04:53:55 +08:00
{
bsd = false;
aoutg_init();
2002-05-01 04:52:49 +08:00
aout_gotpc_sect = aout_gotoff_sect = aout_got_sect =
2005-01-16 06:15:51 +08:00
aout_plt_sect = aout_sym_sect = NO_SEG;
2002-05-01 04:52:49 +08:00
}
#endif
#ifdef OF_AOUTB
extern struct ofmt of_aoutb;
static void aoutb_init(void)
2002-05-01 04:53:55 +08:00
{
bsd = true;
aoutg_init();
2002-05-01 04:52:49 +08:00
2005-01-16 06:15:51 +08:00
is_pic = 0x00; /* may become 0x40 */
2002-05-01 04:52:49 +08:00
aout_gotpc_sect = seg_alloc();
define_label("..gotpc", aout_gotpc_sect + 1, 0L, NULL, false, false);
2002-05-01 04:52:49 +08:00
aout_gotoff_sect = seg_alloc();
define_label("..gotoff", aout_gotoff_sect + 1, 0L, NULL, false, false);
2002-05-01 04:52:49 +08:00
aout_got_sect = seg_alloc();
define_label("..got", aout_got_sect + 1, 0L, NULL, false, false);
2002-05-01 04:52:49 +08:00
aout_plt_sect = seg_alloc();
define_label("..plt", aout_plt_sect + 1, 0L, NULL, false, false);
2002-05-01 04:52:49 +08:00
aout_sym_sect = seg_alloc();
define_label("..sym", aout_sym_sect + 1, 0L, NULL, false, false);
2002-05-01 04:52:49 +08:00
}
#endif
2005-01-16 06:15:51 +08:00
static void aout_cleanup(int debuginfo)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:51:32 +08:00
struct Reloc *r;
2005-01-16 06:15:51 +08:00
(void)debuginfo;
2002-05-01 04:53:55 +08:00
2002-05-01 04:51:32 +08:00
aout_pad_sections();
aout_fixup_relocs(&stext);
aout_fixup_relocs(&sdata);
aout_write();
2005-01-16 06:15:51 +08:00
saa_free(stext.data);
2002-05-01 04:51:32 +08:00
while (stext.head) {
2005-01-16 06:15:51 +08:00
r = stext.head;
stext.head = stext.head->next;
nasm_free(r);
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
saa_free(sdata.data);
2002-05-01 04:51:32 +08:00
while (sdata.head) {
2005-01-16 06:15:51 +08:00
r = sdata.head;
sdata.head = sdata.head->next;
nasm_free(r);
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
saa_free(syms);
raa_free(bsym);
saa_free(strs);
2002-05-01 04:51:32 +08:00
}
static int32_t aout_section_names(char *name, int pass, int *bits)
2002-05-01 04:53:55 +08:00
{
2007-04-14 08:46:25 +08:00
(void)pass;
2002-05-01 04:51:32 +08:00
/*
* Default to 32 bits.
*/
if (!name)
2005-01-16 06:15:51 +08:00
*bits = 32;
2002-05-01 04:51:32 +08:00
if (!name)
2005-01-16 06:15:51 +08:00
return stext.index;
2002-05-01 04:51:32 +08:00
if (!strcmp(name, ".text"))
2005-01-16 06:15:51 +08:00
return stext.index;
2002-05-01 04:51:32 +08:00
else if (!strcmp(name, ".data"))
2005-01-16 06:15:51 +08:00
return sdata.index;
2002-05-01 04:51:32 +08:00
else if (!strcmp(name, ".bss"))
2005-01-16 06:15:51 +08:00
return sbss.index;
2002-05-01 04:51:32 +08:00
else
2005-01-16 06:15:51 +08:00
return NO_SEG;
2002-05-01 04:51:32 +08:00
}
2007-11-06 09:19:32 +08:00
static void aout_deflabel(char *name, int32_t segment, int64_t offset,
int is_global, char *special)
2002-05-01 04:53:55 +08:00
{
2005-01-16 06:15:51 +08:00
int pos = strslen + 4;
2002-05-01 04:51:32 +08:00
struct Symbol *sym;
int special_used = false;
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:26 +08:00
if (name[0] == '.' && name[1] == '.' && name[2] != '@') {
2005-01-16 06:15:51 +08:00
/*
* This is a NASM special symbol. We never allow it into
* the a.out symbol table, even if it's a valid one. If it
* _isn't_ a valid one, we should barf immediately.
*/
if (strcmp(name, "..gotpc") && strcmp(name, "..gotoff") &&
strcmp(name, "..got") && strcmp(name, "..plt") &&
strcmp(name, "..sym"))
nasm_error(ERR_NONFATAL, "unrecognised special symbol `%s'", name);
2005-01-16 06:15:51 +08:00
return;
2002-05-01 04:51:32 +08:00
}
2002-05-01 04:52:49 +08:00
if (is_global == 3) {
2005-01-16 06:15:51 +08:00
struct Symbol **s;
/*
* Fix up a forward-reference symbol size from the first
* pass.
*/
for (s = &fwds; *s; s = &(*s)->nextfwd)
if (!strcmp((*s)->name, name)) {
struct tokenval tokval;
expr *e;
char *p = special;
2005-01-16 06:15:51 +08:00
while (*p && !nasm_isspace(*p))
2005-01-16 06:15:51 +08:00
p++;
while (*p && nasm_isspace(*p))
2005-01-16 06:15:51 +08:00
p++;
stdscan_reset();
stdscan_set(p);
2005-01-16 06:15:51 +08:00
tokval.t_type = TOKEN_INVALID;
e = evaluate(stdscan, NULL, &tokval, NULL, 1, nasm_error, NULL);
2005-01-16 06:15:51 +08:00
if (e) {
if (!is_simple(e))
nasm_error(ERR_NONFATAL, "cannot use relocatable"
2005-01-16 06:15:51 +08:00
" expression as symbol size");
else
(*s)->size = reloc_value(e);
}
/*
* Remove it from the list of unresolved sizes.
*/
nasm_free((*s)->name);
*s = (*s)->nextfwd;
return;
}
return; /* it wasn't an important one */
2002-05-01 04:52:49 +08:00
}
saa_wbytes(strs, name, (int32_t)(1 + strlen(name)));
2005-01-16 06:15:51 +08:00
strslen += 1 + strlen(name);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
sym = saa_wstruct(syms);
2002-05-01 04:51:32 +08:00
sym->strpos = pos;
sym->type = is_global ? SYM_GLOBAL : 0;
2002-05-01 04:52:49 +08:00
sym->segment = segment;
2002-05-01 04:51:32 +08:00
if (segment == NO_SEG)
2005-01-16 06:15:51 +08:00
sym->type |= SECT_ABS;
2002-05-01 04:52:49 +08:00
else if (segment == stext.index) {
2005-01-16 06:15:51 +08:00
sym->type |= SECT_TEXT;
if (is_global) {
sym->next = stext.gsyms;
stext.gsyms = sym;
} else if (!stext.asym)
stext.asym = sym;
2002-05-01 04:52:49 +08:00
} else if (segment == sdata.index) {
2005-01-16 06:15:51 +08:00
sym->type |= SECT_DATA;
if (is_global) {
sym->next = sdata.gsyms;
sdata.gsyms = sym;
} else if (!sdata.asym)
sdata.asym = sym;
2002-05-01 04:52:49 +08:00
} else if (segment == sbss.index) {
2005-01-16 06:15:51 +08:00
sym->type |= SECT_BSS;
if (is_global) {
sym->next = sbss.gsyms;
sbss.gsyms = sym;
} else if (!sbss.asym)
sbss.asym = sym;
2002-05-01 04:52:49 +08:00
} else
2005-01-16 06:15:51 +08:00
sym->type = SYM_GLOBAL;
2002-05-01 04:51:32 +08:00
if (is_global == 2)
2005-01-16 06:15:51 +08:00
sym->value = offset;
2002-05-01 04:51:32 +08:00
else
2005-01-16 06:15:51 +08:00
sym->value = (sym->type == SYM_GLOBAL ? 0 : offset);
2002-05-01 04:51:32 +08:00
2002-05-01 04:52:49 +08:00
if (is_global && sym->type != SYM_GLOBAL) {
2005-01-16 06:15:51 +08:00
/*
* Global symbol exported _from_ this module. We must check
* the special text for type information.
*/
if (special) {
int n = strcspn(special, " ");
if (!nasm_strnicmp(special, "function", n))
sym->type |= SYM_FUNCTION;
else if (!nasm_strnicmp(special, "data", n) ||
!nasm_strnicmp(special, "object", n))
sym->type |= SYM_DATA;
else
nasm_error(ERR_NONFATAL, "unrecognised symbol type `%.*s'",
2005-01-16 06:15:51 +08:00
n, special);
if (special[n]) {
struct tokenval tokval;
expr *e;
int fwd = false;
char *saveme = stdscan_get();
2005-01-16 06:15:51 +08:00
if (!bsd) {
nasm_error(ERR_NONFATAL, "Linux a.out does not support"
2005-01-16 06:15:51 +08:00
" symbol size information");
} else {
while (special[n] && nasm_isspace(special[n]))
2005-01-16 06:15:51 +08:00
n++;
/*
* We have a size expression; attempt to
* evaluate it.
*/
sym->type |= SYM_WITH_SIZE;
stdscan_reset();
stdscan_set(special + n);
2005-01-16 06:15:51 +08:00
tokval.t_type = TOKEN_INVALID;
e = evaluate(stdscan, NULL, &tokval, &fwd, 0, nasm_error,
2005-01-16 06:15:51 +08:00
NULL);
if (fwd) {
sym->nextfwd = fwds;
fwds = sym;
sym->name = nasm_strdup(name);
} else if (e) {
if (!is_simple(e))
nasm_error(ERR_NONFATAL, "cannot use relocatable"
2005-01-16 06:15:51 +08:00
" expression as symbol size");
else
sym->size = reloc_value(e);
}
}
stdscan_set(saveme);
2005-01-16 06:15:51 +08:00
}
special_used = true;
2005-01-16 06:15:51 +08:00
}
2002-05-01 04:52:49 +08:00
}
2002-05-01 04:51:32 +08:00
/*
* define the references from external-symbol segment numbers
* to these symbol records.
*/
if (segment != NO_SEG && segment != stext.index &&
2005-01-16 06:15:51 +08:00
segment != sdata.index && segment != sbss.index)
bsym = raa_write(bsym, segment, nsyms);
2002-05-01 04:52:49 +08:00
sym->symnum = nsyms;
2002-05-01 04:51:32 +08:00
nsyms++;
2002-05-01 04:52:49 +08:00
if (sym->type & SYM_WITH_SIZE)
2005-01-16 06:15:51 +08:00
nsyms++; /* and another for the size */
2002-05-01 04:52:49 +08:00
if (special && !special_used)
nasm_error(ERR_NONFATAL, "no special symbol features supported here");
2002-05-01 04:51:32 +08:00
}
static void aout_add_reloc(struct Section *sect, int32_t segment,
2005-01-16 06:15:51 +08:00
int reltype, int bytes)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:51:32 +08:00
struct Reloc *r;
r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
sect->tail = &r->next;
r->next = NULL;
r->address = sect->len;
r->symbol = (segment == NO_SEG ? -SECT_ABS :
2005-01-16 06:15:51 +08:00
segment == stext.index ? -SECT_TEXT :
segment == sdata.index ? -SECT_DATA :
segment == sbss.index ? -SECT_BSS :
raa_read(bsym, segment));
2002-05-01 04:52:49 +08:00
r->reltype = reltype;
if (r->symbol >= 0)
2005-01-16 06:15:51 +08:00
r->reltype |= RELTYPE_SYMFLAG;
2002-05-01 04:52:49 +08:00
r->bytes = bytes;
sect->nrelocs++;
}
/*
* This routine deals with ..got and ..sym relocations: the more
* complicated kinds. In shared-library writing, some relocations
* with respect to global symbols must refer to the precise symbol
* rather than referring to an offset from the base of the section
* _containing_ the symbol. Such relocations call to this routine,
* which searches the symbol list for the symbol in question.
*
* RELTYPE_GOT references require the _exact_ symbol address to be
* used; RELTYPE_ABSOLUTE references can be at an offset from the
* symbol. The boolean argument `exact' tells us this.
*
* Return value is the adjusted value of `addr', having become an
* offset from the symbol rather than the section. Should always be
* zero when returning from an exact call.
*
* Limitation: if you define two symbols at the same place,
* confusion will occur.
*
* Inefficiency: we search, currently, using a linked list which
* isn't even necessarily sorted.
*/
static int32_t aout_add_gsym_reloc(struct Section *sect,
int32_t segment, int32_t offset,
2005-01-16 06:15:51 +08:00
int type, int bytes, int exact)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:52:49 +08:00
struct Symbol *sym, *sm, *shead;
struct Reloc *r;
/*
* First look up the segment to find whether it's text, data,
* bss or an external symbol.
*/
shead = NULL;
if (segment == stext.index)
2005-01-16 06:15:51 +08:00
shead = stext.gsyms;
2002-05-01 04:52:49 +08:00
else if (segment == sdata.index)
2005-01-16 06:15:51 +08:00
shead = sdata.gsyms;
2002-05-01 04:52:49 +08:00
else if (segment == sbss.index)
2005-01-16 06:15:51 +08:00
shead = sbss.gsyms;
2002-05-01 04:52:49 +08:00
if (!shead) {
2005-01-16 06:15:51 +08:00
if (exact && offset != 0)
nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
2005-01-16 06:15:51 +08:00
" for this reference");
else
aout_add_reloc(sect, segment, type, bytes);
return offset;
2002-05-01 04:52:49 +08:00
}
if (exact) {
2005-01-16 06:15:51 +08:00
/*
* Find a symbol pointing _exactly_ at this one.
*/
list_for_each(sym, shead)
2005-01-16 06:15:51 +08:00
if (sym->value == offset)
break;
2002-05-01 04:52:49 +08:00
} else {
2005-01-16 06:15:51 +08:00
/*
* Find the nearest symbol below this one.
*/
sym = NULL;
list_for_each(sm, shead)
2005-01-16 06:15:51 +08:00
if (sm->value <= offset && (!sym || sm->value > sym->value))
sym = sm;
2002-05-01 04:52:49 +08:00
}
if (!sym && exact) {
nasm_error(ERR_NONFATAL, "unable to find a suitable global symbol"
2005-01-16 06:15:51 +08:00
" for this reference");
return 0;
2002-05-01 04:52:49 +08:00
}
r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
sect->tail = &r->next;
r->next = NULL;
r->address = sect->len;
r->symbol = sym->symnum;
r->reltype = type | RELTYPE_SYMFLAG;
2002-05-01 04:51:32 +08:00
r->bytes = bytes;
sect->nrelocs++;
2002-05-01 04:52:49 +08:00
return offset - sym->value;
}
/*
* This routine deals with ..gotoff relocations. These _must_ refer
* to a symbol, due to a perversity of *BSD's PIC implementation,
* and it must be a non-global one as well; so we store `asym', the
* first nonglobal symbol defined in each section, and always work
* from that. Relocation type is always RELTYPE_GOTOFF.
*
* Return value is the adjusted value of `addr', having become an
* offset from the `asym' symbol rather than the section.
*/
static int32_t aout_add_gotoff_reloc(struct Section *sect, int32_t segment,
int32_t offset, int bytes)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:52:49 +08:00
struct Reloc *r;
struct Symbol *asym;
/*
* First look up the segment to find whether it's text, data,
* bss or an external symbol.
*/
asym = NULL;
if (segment == stext.index)
2005-01-16 06:15:51 +08:00
asym = stext.asym;
2002-05-01 04:52:49 +08:00
else if (segment == sdata.index)
2005-01-16 06:15:51 +08:00
asym = sdata.asym;
2002-05-01 04:52:49 +08:00
else if (segment == sbss.index)
2005-01-16 06:15:51 +08:00
asym = sbss.asym;
2002-05-01 04:52:49 +08:00
if (!asym)
nasm_error(ERR_NONFATAL, "`..gotoff' relocations require a non-global"
2005-01-16 06:15:51 +08:00
" symbol in the section");
2002-05-01 04:52:49 +08:00
r = *sect->tail = nasm_malloc(sizeof(struct Reloc));
sect->tail = &r->next;
r->next = NULL;
r->address = sect->len;
r->symbol = asym->symnum;
r->reltype = RELTYPE_GOTOFF;
r->bytes = bytes;
sect->nrelocs++;
return offset - asym->value;
2002-05-01 04:51:32 +08:00
}
static void aout_out(int32_t segto, const void *data,
enum out_type type, uint64_t size,
int32_t segment, int32_t wrt)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:51:32 +08:00
struct Section *s;
int32_t addr;
uint8_t mydata[4], *p;
2002-05-01 04:51:32 +08:00
/*
* handle absolute-assembly (structure definitions)
*/
if (segto == NO_SEG) {
2005-01-16 06:15:51 +08:00
if (type != OUT_RESERVE)
nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
2005-01-16 06:15:51 +08:00
" space");
return;
2002-05-01 04:51:32 +08:00
}
if (segto == stext.index)
2005-01-16 06:15:51 +08:00
s = &stext;
2002-05-01 04:51:32 +08:00
else if (segto == sdata.index)
2005-01-16 06:15:51 +08:00
s = &sdata;
2002-05-01 04:52:49 +08:00
else if (segto == sbss.index)
2005-01-16 06:15:51 +08:00
s = NULL;
2002-05-01 04:51:32 +08:00
else {
nasm_error(ERR_WARNING, "attempt to assemble code in"
2005-01-16 06:15:51 +08:00
" segment %d: defaulting to `.text'", segto);
s = &stext;
2002-05-01 04:51:32 +08:00
}
if (!s && type != OUT_RESERVE) {
nasm_error(ERR_WARNING, "attempt to initialize memory in the"
2005-01-16 06:15:51 +08:00
" BSS section: ignored");
sbss.len += realsize(type, size);
2005-01-16 06:15:51 +08:00
return;
2002-05-01 04:51:32 +08:00
}
if (type == OUT_RESERVE) {
2005-01-16 06:15:51 +08:00
if (s) {
nasm_error(ERR_WARNING, "uninitialized space declared in"
2005-01-16 06:15:51 +08:00
" %s section: zeroing",
(segto == stext.index ? "code" : "data"));
aout_sect_write(s, NULL, size);
2005-01-16 06:15:51 +08:00
} else
sbss.len += size;
2002-05-01 04:51:32 +08:00
} else if (type == OUT_RAWDATA) {
2005-01-16 06:15:51 +08:00
if (segment != NO_SEG)
nasm_error(ERR_PANIC, "OUT_RAWDATA with other than NO_SEG");
aout_sect_write(s, data, size);
2002-05-01 04:51:32 +08:00
} else if (type == OUT_ADDRESS) {
addr = *(int64_t *)data;
2005-01-16 06:15:51 +08:00
if (segment != NO_SEG) {
if (segment % 2) {
nasm_error(ERR_NONFATAL, "a.out format does not support"
2005-01-16 06:15:51 +08:00
" segment base references");
} else {
if (wrt == NO_SEG) {
aout_add_reloc(s, segment, RELTYPE_ABSOLUTE,
size);
2005-01-16 06:15:51 +08:00
} else if (!bsd) {
nasm_error(ERR_NONFATAL,
2005-01-16 06:15:51 +08:00
"Linux a.out format does not support"
" any use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
} else if (wrt == aout_gotpc_sect + 1) {
is_pic = 0x40;
aout_add_reloc(s, segment, RELTYPE_GOTPC, size);
2005-01-16 06:15:51 +08:00
} else if (wrt == aout_gotoff_sect + 1) {
is_pic = 0x40;
addr = aout_add_gotoff_reloc(s, segment,
addr, size);
2005-01-16 06:15:51 +08:00
} else if (wrt == aout_got_sect + 1) {
is_pic = 0x40;
addr =
aout_add_gsym_reloc(s, segment, addr, RELTYPE_GOT,
size, true);
2005-01-16 06:15:51 +08:00
} else if (wrt == aout_sym_sect + 1) {
addr = aout_add_gsym_reloc(s, segment, addr,
RELTYPE_ABSOLUTE, size,
false);
2005-01-16 06:15:51 +08:00
} else if (wrt == aout_plt_sect + 1) {
is_pic = 0x40;
nasm_error(ERR_NONFATAL,
2005-01-16 06:15:51 +08:00
"a.out format cannot produce non-PC-"
"relative PLT references");
} else {
nasm_error(ERR_NONFATAL,
2005-01-16 06:15:51 +08:00
"a.out format does not support this"
" use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
}
}
}
p = mydata;
if (size == 2)
2005-01-16 06:15:51 +08:00
WRITESHORT(p, addr);
else
WRITELONG(p, addr);
aout_sect_write(s, mydata, size);
2002-05-01 04:51:32 +08:00
} else if (type == OUT_REL2ADR) {
2005-01-16 06:15:51 +08:00
if (segment == segto)
nasm_error(ERR_PANIC, "intra-segment OUT_REL2ADR");
2005-01-16 06:15:51 +08:00
if (segment != NO_SEG && segment % 2) {
nasm_error(ERR_NONFATAL, "a.out format does not support"
2005-01-16 06:15:51 +08:00
" segment base references");
} else {
if (wrt == NO_SEG) {
aout_add_reloc(s, segment, RELTYPE_RELATIVE, 2);
} else if (!bsd) {
nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
2005-01-16 06:15:51 +08:00
" any use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
} else if (wrt == aout_plt_sect + 1) {
is_pic = 0x40;
aout_add_reloc(s, segment, RELTYPE_PLT, 2);
} else if (wrt == aout_gotpc_sect + 1 ||
wrt == aout_gotoff_sect + 1 ||
wrt == aout_got_sect + 1) {
nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
2005-01-16 06:15:51 +08:00
"relative GOT references");
} else {
nasm_error(ERR_NONFATAL, "a.out format does not support this"
2005-01-16 06:15:51 +08:00
" use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
}
}
p = mydata;
WRITESHORT(p, *(int64_t *)data - (size + s->len));
2005-01-16 06:15:51 +08:00
aout_sect_write(s, mydata, 2L);
2002-05-01 04:51:32 +08:00
} else if (type == OUT_REL4ADR) {
2005-01-16 06:15:51 +08:00
if (segment == segto)
nasm_error(ERR_PANIC, "intra-segment OUT_REL4ADR");
2005-01-16 06:15:51 +08:00
if (segment != NO_SEG && segment % 2) {
nasm_error(ERR_NONFATAL, "a.out format does not support"
2005-01-16 06:15:51 +08:00
" segment base references");
} else {
if (wrt == NO_SEG) {
aout_add_reloc(s, segment, RELTYPE_RELATIVE, 4);
} else if (!bsd) {
nasm_error(ERR_NONFATAL, "Linux a.out format does not support"
2005-01-16 06:15:51 +08:00
" any use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
} else if (wrt == aout_plt_sect + 1) {
is_pic = 0x40;
aout_add_reloc(s, segment, RELTYPE_PLT, 4);
} else if (wrt == aout_gotpc_sect + 1 ||
wrt == aout_gotoff_sect + 1 ||
wrt == aout_got_sect + 1) {
nasm_error(ERR_NONFATAL, "a.out format cannot produce PC-"
2005-01-16 06:15:51 +08:00
"relative GOT references");
} else {
nasm_error(ERR_NONFATAL, "a.out format does not support this"
2005-01-16 06:15:51 +08:00
" use of WRT");
wrt = NO_SEG; /* we can at least _try_ to continue */
}
}
p = mydata;
WRITELONG(p, *(int64_t *)data - (size + s->len));
2005-01-16 06:15:51 +08:00
aout_sect_write(s, mydata, 4L);
2002-05-01 04:51:32 +08:00
}
}
2005-01-16 06:15:51 +08:00
static void aout_pad_sections(void)
2002-05-01 04:53:55 +08:00
{
static uint8_t pad[] = { 0x90, 0x90, 0x90, 0x90 };
2002-05-01 04:51:32 +08:00
/*
* Pad each of the text and data sections with NOPs until their
* length is a multiple of four. (NOP == 0x90.) Also increase
* the length of the BSS section similarly.
*/
aout_sect_write(&stext, pad, (-(int32_t)stext.len) & 3);
aout_sect_write(&sdata, pad, (-(int32_t)sdata.len) & 3);
2002-05-01 04:52:49 +08:00
sbss.len = (sbss.len + 3) & ~3;
2002-05-01 04:51:32 +08:00
}
/*
* a.out files have the curious property that all references to
* things in the data or bss sections are done by addresses which
* are actually relative to the start of the _text_ section, in the
* _file_. (No relation to what happens after linking. No idea why
* this should be so. It's very strange.) So we have to go through
* the relocation table, _after_ the final size of each section is
* known, and fix up the relocations pointed to.
*/
2005-01-16 06:15:51 +08:00
static void aout_fixup_relocs(struct Section *sect)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:51:32 +08:00
struct Reloc *r;
2005-01-16 06:15:51 +08:00
saa_rewind(sect->data);
list_for_each(r, sect->head) {
uint8_t *p, *q, blk[4];
int32_t l;
2005-01-16 06:15:51 +08:00
saa_fread(sect->data, r->address, blk, (int32_t)r->bytes);
2005-01-16 06:15:51 +08:00
p = q = blk;
l = *p++;
if (r->bytes > 1) {
l += ((int32_t)*p++) << 8;
2005-01-16 06:15:51 +08:00
if (r->bytes == 4) {
l += ((int32_t)*p++) << 16;
l += ((int32_t)*p++) << 24;
2005-01-16 06:15:51 +08:00
}
}
if (r->symbol == -SECT_DATA)
l += stext.len;
else if (r->symbol == -SECT_BSS)
l += stext.len + sdata.len;
if (r->bytes == 4)
WRITELONG(q, l);
else if (r->bytes == 2)
WRITESHORT(q, l);
else
*q++ = l & 0xFF;
saa_fwrite(sect->data, r->address, blk, (int32_t)r->bytes);
2002-05-01 04:51:32 +08:00
}
}
2005-01-16 06:15:51 +08:00
static void aout_write(void)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:51:32 +08:00
/*
* Emit the a.out header.
*/
2002-05-01 04:52:49 +08:00
/* OMAGIC, M_386 or MID_I386, no flags */
fwriteint32_t(bsd ? 0x07018600 | is_pic : 0x640107L, ofile);
fwriteint32_t(stext.len, ofile);
fwriteint32_t(sdata.len, ofile);
fwriteint32_t(sbss.len, ofile);
fwriteint32_t(nsyms * 12, ofile); /* length of symbol table */
fwriteint32_t(0L, ofile); /* object files have no entry point */
fwriteint32_t(stext.nrelocs * 8, ofile); /* size of text relocs */
fwriteint32_t(sdata.nrelocs * 8, ofile); /* size of data relocs */
2002-05-01 04:51:32 +08:00
/*
* Write out the code section and the data section.
*/
saa_fpwrite(stext.data, ofile);
saa_fpwrite(sdata.data, ofile);
2002-05-01 04:51:32 +08:00
/*
* Write out the relocations.
*/
2005-01-16 06:15:51 +08:00
aout_write_relocs(stext.head);
aout_write_relocs(sdata.head);
2002-05-01 04:51:32 +08:00
/*
* Write the symbol table.
*/
2005-01-16 06:15:51 +08:00
aout_write_syms();
2002-05-01 04:51:32 +08:00
/*
* And the string table.
*/
fwriteint32_t(strslen + 4, ofile); /* length includes length count */
saa_fpwrite(strs, ofile);
2002-05-01 04:51:32 +08:00
}
2005-01-16 06:15:51 +08:00
static void aout_write_relocs(struct Reloc *r)
2002-05-01 04:53:55 +08:00
{
list_for_each(r, r) {
uint32_t word2;
2002-05-01 04:51:32 +08:00
fwriteint32_t(r->address, ofile);
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
if (r->symbol >= 0)
word2 = r->symbol;
else
word2 = -r->symbol;
word2 |= r->reltype << 24;
word2 |= (r->bytes == 1 ? 0 :
r->bytes == 2 ? 0x2000000L : 0x4000000L);
fwriteint32_t(word2, ofile);
2002-05-01 04:51:32 +08:00
}
}
2005-01-16 06:15:51 +08:00
static void aout_write_syms(void)
2002-05-01 04:53:55 +08:00
{
uint32_t i;
2002-05-01 04:51:32 +08:00
2005-01-16 06:15:51 +08:00
saa_rewind(syms);
2002-05-01 05:01:08 +08:00
for (i = 0; i < nsyms; i++) {
2005-01-16 06:15:51 +08:00
struct Symbol *sym = saa_rstruct(syms);
fwriteint32_t(sym->strpos, ofile);
fwriteint32_t((int32_t)sym->type & ~SYM_WITH_SIZE, ofile);
2005-01-16 06:15:51 +08:00
/*
* Fix up the symbol value now we know the final section
* sizes.
*/
if ((sym->type & SECT_MASK) == SECT_DATA)
sym->value += stext.len;
if ((sym->type & SECT_MASK) == SECT_BSS)
sym->value += stext.len + sdata.len;
fwriteint32_t(sym->value, ofile);
2005-01-16 06:15:51 +08:00
/*
* Output a size record if necessary.
*/
if (sym->type & SYM_WITH_SIZE) {
fwriteint32_t(sym->strpos, ofile);
fwriteint32_t(0x0DL, ofile); /* special value: means size */
fwriteint32_t(sym->size, ofile);
2005-01-16 06:15:51 +08:00
i++; /* use up another of `nsyms' */
}
2002-05-01 04:51:32 +08:00
}
}
2005-01-16 06:15:51 +08:00
static void aout_sect_write(struct Section *sect,
const uint8_t *data, uint32_t len)
2002-05-01 04:53:55 +08:00
{
2005-01-16 06:15:51 +08:00
saa_wbytes(sect->data, data, len);
2002-05-01 04:51:32 +08:00
sect->len += len;
}
static int32_t aout_segbase(int32_t segment)
2002-05-01 04:53:55 +08:00
{
2002-05-01 04:51:32 +08:00
return segment;
}
static void aout_filename(char *inname, char *outname)
2002-05-01 04:53:55 +08:00
{
standard_extension(inname, outname, ".o");
2002-05-01 04:51:32 +08:00
}
extern macros_t aout_stdmac[];
2002-05-01 04:52:49 +08:00
2005-01-16 06:15:51 +08:00
#endif /* OF_AOUT || OF_AOUTB */
2002-05-01 04:52:49 +08:00
#ifdef OF_AOUT
2002-05-01 04:51:32 +08:00
struct ofmt of_aout = {
2002-05-01 04:52:49 +08:00
"Linux a.out object files",
2002-05-01 04:51:32 +08:00
"aout",
0,
2002-05-01 04:53:55 +08:00
null_debug_arr,
&null_debug_form,
2002-05-01 04:52:49 +08:00
aout_stdmac,
2002-05-01 04:51:32 +08:00
aout_init,
null_setinfo,
2002-05-01 04:51:32 +08:00
aout_out,
aout_deflabel,
aout_section_names,
aout_segbase,
null_directive,
2002-05-01 04:51:32 +08:00
aout_filename,
aout_cleanup
};
2002-05-01 04:52:49 +08:00
#endif
#ifdef OF_AOUTB
struct ofmt of_aoutb = {
"NetBSD/FreeBSD a.out object files",
"aoutb",
0,
2002-05-01 04:53:55 +08:00
null_debug_arr,
&null_debug_form,
2002-05-01 04:52:49 +08:00
aout_stdmac,
aoutb_init,
null_setinfo,
2002-05-01 04:52:49 +08:00
aout_out,
aout_deflabel,
aout_section_names,
aout_segbase,
null_directive,
2002-05-01 04:52:49 +08:00
aout_filename,
aout_cleanup
};
#endif