#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
#include "libiberty.h"
#include "elf/mmix.h"
#include "opcode/mmix.h"
#define LOP 0x98
#define LOP_QUOTE 0
#define LOP_LOC 1
#define LOP_SKIP 2
#define LOP_FIXO 3
#define LOP_FIXR 4
#define LOP_FIXRX 5
#define LOP_FILE 6
#define LOP_LINE 7
#define LOP_SPEC 8
#define LOP_PRE 9
#define LOP_POST 10
#define LOP_STAB 11
#define LOP_END 12
#define LOP_QUOTE_NEXT ((LOP << 24) | (LOP_QUOTE << 16) | 1)
#define SPEC_DATA_SECTION 80
#define LOP_SPEC_SECTION \
((LOP << 24) | (LOP_SPEC << 16) | SPEC_DATA_SECTION)
#define MMO_SEC_CONTENTS_CHUNK_SIZE (1 << 15)
#define MAX_SECTION_NAME_SIZE (1024 * 1024)
#define MAX_ARTIFICIAL_SECTION_SIZE (1024 * 1024 * 1024)
#define MMO3_WCHAR 0x80
#define MMO3_LEFT 0x40
#define MMO3_MIDDLE 0x20
#define MMO3_RIGHT 0x10
#define MMO3_TYPEBITS 0xf
#define MMO3_REGQUAL_BITS 0xf
#define MMO3_UNDEF 2
#define MMO3_DATA 8
#define MMO3_SYMBITS 0x2f
#define FATAL_DEBUG \
_bfd_abort (__FILE__, __LINE__, \
"Internal: Non-debugged code (test-case missing)")
#define BAD_CASE(x) \
_bfd_abort (__FILE__, __LINE__, \
"bad case for " #x)
enum mmo_sym_type { mmo_reg_sym, mmo_undef_sym, mmo_data_sym, mmo_abs_sym};
struct mmo_symbol
{
struct mmo_symbol *next;
char *name;
bfd_vma value;
enum mmo_sym_type sym_type;
unsigned int serno;
};
struct mmo_data_list_struct
{
struct mmo_data_list_struct *next;
bfd_vma where;
bfd_size_type size;
bfd_size_type allocated_size;
bfd_byte data[1];
};
typedef struct mmo_data_list_struct mmo_data_list_type;
struct mmo_symbol_trie
{
struct mmo_symbol_trie *left;
struct mmo_symbol_trie *right;
struct mmo_symbol_trie *middle;
bfd_byte symchar;
struct mmo_symbol sym;
};
struct mmo_data_struct
{
struct mmo_symbol *symbols;
struct mmo_symbol *symtail;
asymbol *csymbols;
bfd_byte created[4];
bfd_boolean have_error;
int max_symbol_length;
char *lop_stab_symbol;
int symbol_position;
int sec_no;
int byte_no;
bfd_byte buf[4];
};
typedef struct mmo_data_struct tdata_type;
struct mmo_section_data_struct
{
mmo_data_list_type *head;
mmo_data_list_type *tail;
};
#define mmo_section_data(sec) \
((struct mmo_section_data_struct *) (sec)->used_by_bfd)
struct mmo_write_sec_info
{
asection *reg_section;
bfd_boolean retval;
};
struct mmo_find_sec_info
{
asection *sec;
bfd_vma addr;
};
static bfd_boolean mmo_bfd_copy_private_bfd_data (bfd *, bfd *);
static void mmo_write_section_unless_reg_contents (bfd *, asection *, void *);
static void mmo_find_sec_w_addr (bfd *, asection *, void *);
static void mmo_find_sec_w_addr_grow (bfd *, asection *, void *);
static asection *mmo_make_section (bfd *, const char *);
static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
static void mmo_print_symbol (bfd *, void *, asymbol *,
bfd_print_symbol_type);
static void mmo_init (void);
static bfd_boolean mmo_mkobject (bfd *);
static bfd_boolean mmo_scan (bfd *);
static asection *mmo_decide_section (bfd *, bfd_vma);
static asection *mmo_get_generic_spec_data_section (bfd *, int);
static asection *mmo_get_spec_section (bfd *, int);
static INLINE bfd_byte *mmo_get_loc (asection *, bfd_vma, int);
static void mmo_xore_64 (asection *, bfd_vma vma, bfd_vma value);
static void mmo_xore_32 (asection *, bfd_vma vma, unsigned int);
static void mmo_xore_16 (asection *, bfd_vma vma, unsigned int);
static const bfd_target *mmo_object_p (bfd *);
static void mmo_map_set_sizes (bfd *, asection *, void *);
static bfd_boolean mmo_get_symbols (bfd *);
static bfd_boolean mmo_create_symbol (bfd *, const char *, bfd_vma,
enum mmo_sym_type, unsigned int);
static bfd_boolean mmo_get_section_contents (bfd *, asection *, void *,
file_ptr, bfd_size_type);
static long mmo_get_symtab_upper_bound (bfd *);
static long mmo_canonicalize_symtab (bfd *, asymbol **);
static void mmo_get_symbol_info (bfd *, asymbol *, symbol_info *);
static void mmo_print_symbol (bfd *, void *, asymbol *,
bfd_print_symbol_type);
static bfd_boolean mmo_set_section_contents (bfd *, sec_ptr, const void *,
file_ptr, bfd_size_type);
static int mmo_sizeof_headers (bfd *, bfd_boolean);
static long mmo_get_reloc_upper_bound (bfd *, asection *);
static bfd_boolean mmo_internal_write_header (bfd *);
static bfd_boolean mmo_internal_write_post (bfd *, int, asection *);
static bfd_boolean mmo_internal_add_3_sym (bfd *, struct mmo_symbol_trie *,
const struct mmo_symbol *);
static unsigned int mmo_internal_3_length (bfd *, struct mmo_symbol_trie *);
static void mmo_internal_3_dump (bfd *, struct mmo_symbol_trie *);
static void mmo_beb128_out (bfd *, int, int);
static bfd_boolean mmo_internal_write_section (bfd *, asection *);
static void mmo_write_tetra (bfd *, unsigned int);
static void mmo_write_tetra_raw (bfd *, unsigned int);
static void mmo_write_octa (bfd *, bfd_vma);
static void mmo_write_octa_raw (bfd *, bfd_vma);
static bfd_boolean mmo_write_chunk (bfd *, const bfd_byte *, unsigned int);
static bfd_boolean mmo_flush_chunk (bfd *);
static bfd_boolean mmo_write_loc_chunk (bfd *, bfd_vma, const bfd_byte *,
unsigned int, bfd_vma *);
static bfd_boolean mmo_write_chunk_list (bfd *, mmo_data_list_type *);
static bfd_boolean mmo_write_loc_chunk_list (bfd *, mmo_data_list_type *);
static bfd_boolean mmo_write_symbols_and_terminator (bfd *);
static flagword mmo_sec_flags_from_bfd_flags (flagword);
static flagword bfd_sec_flags_from_mmo_flags (flagword);
static bfd_byte mmo_get_byte (bfd *);
static void mmo_write_byte (bfd *, bfd_byte);
static bfd_boolean mmo_new_section_hook (bfd *, asection *);
static int mmo_sort_mmo_symbols (const void *, const void *);
static bfd_boolean mmo_write_object_contents (bfd *);
static long mmo_canonicalize_reloc (bfd *, sec_ptr, arelent **, asymbol **);
static bfd_boolean mmo_write_section_description (bfd *, asection *);
static bfd_boolean mmo_has_leading_or_trailing_zero_tetra_p (bfd *,
asection *);
static
char valid_mmo_symbol_character_set[
+ 'Z' - 'A' + 1 + 'z' - 'a' + 1
+ 10
+ 1 + 1
+ 256 - 126
+ 1];
static asection *
mmo_make_section (bfd *abfd, const char *secname)
{
asection *sec = bfd_get_section_by_name (abfd, secname);
if (sec == NULL)
{
char *newsecname = strdup (secname);
if (newsecname == NULL)
{
(*_bfd_error_handler)
(_("%s: No core to allocate section name %s\n"),
bfd_get_filename (abfd), secname);
bfd_set_error (bfd_error_system_call);
return NULL;
}
sec = bfd_make_section (abfd, newsecname);
}
return sec;
}
static void
mmo_init (void)
{
static bfd_boolean inited = FALSE;
int i = 0;
int j = 0;
static const char letters[]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789:_";
if (inited)
return;
inited = TRUE;
strcpy (valid_mmo_symbol_character_set, letters);
i = strlen (letters);
for (j = 126; j < 256; j++)
valid_mmo_symbol_character_set[i++] = j;
}
static const bfd_target *
mmo_object_p (bfd *abfd)
{
struct stat statbuf;
bfd_byte b[4];
mmo_init ();
if (bfd_stat (abfd, &statbuf) < 0
|| bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
|| bfd_bread (b, 4, abfd) != 4)
goto bad_final;
if ((statbuf.st_size % 4) != 0
|| b[0] != LOP || b[1] != LOP_PRE || b[2] != 1)
goto bad_format;
if (bfd_seek (abfd, (file_ptr) statbuf.st_size - 4, SEEK_SET) != 0
|| bfd_bread (b, 4, abfd) != 4)
goto bad_final;
if (b[0] != LOP || b[1] != LOP_END || ! mmo_mkobject (abfd))
goto bad_format;
abfd->tdata.mmo_data->max_symbol_length = (b[2] * 256 + b[3]) * 4;
abfd->tdata.mmo_data->lop_stab_symbol
= bfd_malloc (abfd->tdata.mmo_data->max_symbol_length + 1);
if (abfd->tdata.mmo_data->lop_stab_symbol == NULL)
{
(*_bfd_error_handler)
(_("%s: No core to allocate a symbol %d bytes long\n"),
bfd_get_filename (abfd), abfd->tdata.mmo_data->max_symbol_length);
goto bad_final;
}
if (! mmo_scan (abfd))
goto bad_format_free;
if (abfd->symcount > 0)
abfd->flags |= HAS_SYMS;
if (! bfd_default_set_arch_mach (abfd, bfd_arch_mmix, 0))
goto bad_format_free;
return abfd->xvec;
bad_format_free:
free (abfd->tdata.mmo_data->lop_stab_symbol);
bad_format:
bfd_set_error (bfd_error_wrong_format);
bad_final:
return NULL;
}
static bfd_boolean
mmo_mkobject (bfd *abfd)
{
mmo_init ();
if (abfd->tdata.mmo_data == NULL)
{
time_t created;
tdata_type *tdata = (tdata_type *) bfd_zmalloc (sizeof (tdata_type));
if (tdata == NULL)
return FALSE;
created = time (NULL);
bfd_put_32 (abfd, created, tdata->created);
abfd->tdata.mmo_data = tdata;
}
return TRUE;
}
static bfd_boolean
mmo_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
{
if (bfd_get_flavour (ibfd) != bfd_target_mmo_flavour
|| bfd_get_flavour (obfd) != bfd_target_mmo_flavour)
return TRUE;
memcpy (obfd->tdata.mmo_data->created, ibfd->tdata.mmo_data->created,
sizeof (obfd->tdata.mmo_data->created));
return TRUE;
}
static void
mmo_find_sec_w_addr (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
{
struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
bfd_vma vma = bfd_get_section_vma (abfd, sec);
if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
!= (SEC_LOAD | SEC_ALLOC))
return;
if (infop->addr >= vma && infop->addr < vma + sec->size)
infop->sec = sec;
}
static void
mmo_find_sec_w_addr_grow (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *p)
{
struct mmo_find_sec_info *infop = (struct mmo_find_sec_info *) p;
bfd_vma vma = bfd_get_section_vma (abfd, sec);
if ((bfd_get_section_flags (abfd, sec) & (SEC_LOAD | SEC_ALLOC))
!= (SEC_LOAD | SEC_ALLOC))
return;
if (infop->addr >= vma && infop->addr < vma + MAX_ARTIFICIAL_SECTION_SIZE)
infop->sec = sec;
}
static asection *
mmo_decide_section (bfd *abfd, bfd_vma vma)
{
asection *sec = NULL;
char sec_name[sizeof (".MMIX.sec.") + 20];
struct mmo_find_sec_info info;
info.addr = vma;
info.sec = NULL;
bfd_map_over_sections (abfd, mmo_find_sec_w_addr, &info);
if (info.sec != NULL)
return info.sec;
if ((vma >> 56) == 0)
{
sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
if (sec == NULL)
return NULL;
if (! sec->user_set_vma)
bfd_set_section_vma (abfd, sec, vma);
if (! bfd_set_section_flags (abfd, sec,
bfd_get_section_flags (abfd, sec)
| SEC_CODE | SEC_LOAD | SEC_ALLOC))
return NULL;
}
else if ((vma >> 56) == 0x20)
{
sec = bfd_make_section_old_way (abfd, MMO_DATA_SECTION_NAME);
if (sec == NULL)
return NULL;
if (! sec->user_set_vma)
bfd_set_section_vma (abfd, sec, vma);
if (! bfd_set_section_flags (abfd, sec,
bfd_get_section_flags (abfd, sec)
| SEC_LOAD | SEC_ALLOC))
return NULL;
}
bfd_map_over_sections (abfd, mmo_find_sec_w_addr_grow, &info);
if (info.sec != NULL)
return info.sec;
sprintf (sec_name, ".MMIX.sec.%d", abfd->tdata.mmo_data->sec_no++);
sec = mmo_make_section (abfd, sec_name);
if (! sec->user_set_vma)
bfd_set_section_vma (abfd, sec, vma);
if (! bfd_set_section_flags (abfd, sec,
bfd_get_section_flags (abfd, sec)
| SEC_LOAD | SEC_ALLOC))
return NULL;
return sec;
}
static INLINE void
mmo_xore_64 (asection *sec, bfd_vma vma, bfd_vma value)
{
bfd_byte *loc = mmo_get_loc (sec, vma, 8);
bfd_vma prev = bfd_get_64 (sec->owner, loc);
value ^= prev;
bfd_put_64 (sec->owner, value, loc);
}
static INLINE void
mmo_xore_32 (asection *sec, bfd_vma vma, unsigned int value)
{
bfd_byte *loc = mmo_get_loc (sec, vma, 4);
unsigned int prev = bfd_get_32 (sec->owner, loc);
value ^= prev;
bfd_put_32 (sec->owner, value, loc);
}
static INLINE void
mmo_xore_16 (asection *sec, bfd_vma vma, unsigned int value)
{
bfd_byte *loc = mmo_get_loc (sec, vma, 2);
unsigned int prev = bfd_get_16 (sec->owner, loc);
value ^= prev;
bfd_put_16 (sec->owner, value, loc);
}
static INLINE void
mmo_write_tetra_raw (bfd *abfd, unsigned int value)
{
bfd_byte buf[4];
bfd_put_32 (abfd, value, buf);
if (bfd_bwrite (buf, 4, abfd) != 4)
abfd->tdata.mmo_data->have_error = TRUE;
}
static INLINE void
mmo_write_tetra (bfd *abfd, unsigned int value)
{
if (((value >> 24) & 0xff) == LOP)
mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
mmo_write_tetra_raw (abfd, value);
}
static INLINE void
mmo_write_octa (bfd *abfd, bfd_vma value)
{
mmo_write_tetra (abfd, (unsigned int) (value >> 32));
mmo_write_tetra (abfd, (unsigned int) value);
}
static INLINE void
mmo_write_octa_raw (bfd *abfd, bfd_vma value)
{
mmo_write_tetra_raw (abfd, (unsigned int) (value >> 32));
mmo_write_tetra_raw (abfd, (unsigned int) value);
}
static INLINE bfd_boolean
mmo_write_chunk (bfd *abfd, const bfd_byte *loc, unsigned int len)
{
bfd_boolean retval = TRUE;
if (abfd->tdata.mmo_data->byte_no != 0)
{
while (abfd->tdata.mmo_data->byte_no < 4 && len != 0)
{
abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no++] = *loc++;
len--;
}
if (abfd->tdata.mmo_data->byte_no == 4)
{
mmo_write_tetra (abfd,
bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
abfd->tdata.mmo_data->byte_no = 0;
}
}
while (len >= 4)
{
if (loc[0] == LOP)
mmo_write_tetra_raw (abfd, LOP_QUOTE_NEXT);
retval = (retval
&& ! abfd->tdata.mmo_data->have_error
&& 4 == bfd_bwrite (loc, 4, abfd));
loc += 4;
len -= 4;
}
if (len)
{
memcpy (abfd->tdata.mmo_data->buf, loc, len);
abfd->tdata.mmo_data->byte_no = len;
}
if (! retval)
abfd->tdata.mmo_data->have_error = TRUE;
return retval;
}
static INLINE bfd_boolean
mmo_flush_chunk (bfd *abfd)
{
if (abfd->tdata.mmo_data->byte_no != 0)
{
memset (abfd->tdata.mmo_data->buf + abfd->tdata.mmo_data->byte_no,
0, 4 - abfd->tdata.mmo_data->byte_no);
mmo_write_tetra (abfd,
bfd_get_32 (abfd, abfd->tdata.mmo_data->buf));
abfd->tdata.mmo_data->byte_no = 0;
}
return ! abfd->tdata.mmo_data->have_error;
}
static INLINE bfd_boolean
mmo_write_chunk_list (bfd *abfd, mmo_data_list_type *datap)
{
for (; datap != NULL; datap = datap->next)
if (! mmo_write_chunk (abfd, datap->data, datap->size))
return FALSE;
return mmo_flush_chunk (abfd);
}
static bfd_boolean
mmo_write_loc_chunk (bfd *abfd, bfd_vma vma, const bfd_byte *loc,
unsigned int len, bfd_vma *last_vmap)
{
if (abfd->tdata.mmo_data->byte_no == 0 || vma != *last_vmap)
{
while (len >= 4 && bfd_get_32 (abfd, loc) == 0)
{
vma += 4;
len -= 4;
loc += 4;
}
while (len >= 4 && bfd_get_32 (abfd, loc + len - 4) == 0)
len -= 4;
}
if (vma != *last_vmap)
{
mmo_flush_chunk (abfd);
mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_LOC << 16) | 2);
mmo_write_octa_raw (abfd, vma);
}
*last_vmap = vma + len;
return (! abfd->tdata.mmo_data->have_error
&& mmo_write_chunk (abfd, loc, len));
}
static INLINE bfd_boolean
mmo_write_loc_chunk_list (bfd *abfd, mmo_data_list_type *datap)
{
bfd_vma last_vma = datap ? datap->where - 1 : 0;
for (; datap != NULL; datap = datap->next)
if (! mmo_write_loc_chunk (abfd, datap->where, datap->data, datap->size,
&last_vma))
return FALSE;
return mmo_flush_chunk (abfd);
}
static asection *
mmo_get_generic_spec_data_section (bfd *abfd, int spec_data_number)
{
asection *sec;
char secname[sizeof (MMIX_OTHER_SPEC_SECTION_PREFIX) + 20]
= MMIX_OTHER_SPEC_SECTION_PREFIX;
sprintf (secname + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX),
"%d", spec_data_number);
sec = mmo_make_section (abfd, secname);
return sec;
}
static asection *
mmo_get_spec_section (bfd *abfd, int spec_data_number)
{
char *secname;
asection *sec;
bfd_byte buf[4];
unsigned int secname_length;
unsigned int i;
bfd_vma section_length;
bfd_vma section_vma;
mmo_data_list_type *loc;
flagword flags;
long orig_pos;
if (spec_data_number != SPEC_DATA_SECTION)
return mmo_get_generic_spec_data_section (abfd, spec_data_number);
orig_pos = bfd_tell (abfd);
if (bfd_bread (buf, 4, abfd) != 4)
goto format_error;
if (buf[0] == LOP)
{
if (buf[1] != LOP_QUOTE)
goto format_error;
if (bfd_bread (buf, 4, abfd) != 4)
goto format_error;
}
secname_length = bfd_get_32 (abfd, buf) * 4;
if (secname_length > MAX_SECTION_NAME_SIZE)
goto format_error;
secname = bfd_malloc (secname_length + 1);
secname[secname_length] = 0;
for (i = 0; i < secname_length / 4; i++)
{
if (bfd_bread (secname + i * 4, 4, abfd) != 4)
goto format_error_free;
if (secname[i * 4] == (char) LOP)
{
if (secname[i * 4 + 1] != LOP_QUOTE
|| bfd_bread (secname + i * 4, 4, abfd) != 4)
goto format_error_free;
}
}
if (bfd_bread (buf, 4, abfd) != 4
|| (buf[0] == LOP
&& (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
goto format_error_free;
flags = bfd_get_32 (abfd, buf);
if (bfd_bread (buf, 4, abfd) != 4
|| (buf[0] == LOP
&& (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
goto format_error_free;
section_length = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
if (bfd_bread (buf, 4, abfd) != 4
|| (buf[0] == LOP
&& (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
goto format_error_free;
section_length |= (bfd_vma) bfd_get_32 (abfd, buf);
if (section_length > MAX_ARTIFICIAL_SECTION_SIZE)
goto format_error_free;
if (bfd_bread (buf, 4, abfd) != 4
|| (buf[0] == LOP
&& (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
goto format_error_free;
section_vma = (bfd_vma) bfd_get_32 (abfd, buf) << 32;
if (bfd_bread (buf, 4, abfd) != 4
|| (buf[0] == LOP
&& (buf[1] != LOP_QUOTE || bfd_bread (buf, 4, abfd) != 4)))
goto format_error_free;
section_vma |= (bfd_vma) bfd_get_32 (abfd, buf);
sec = mmo_make_section (abfd, secname);
free (secname);
if (sec == NULL)
goto format_error;
loc = bfd_zmalloc (section_length + 3
+ sizeof (struct mmo_data_list_struct));
if (loc == NULL)
goto format_error;
loc->size = (section_length + 3) & ~3;
if (! bfd_set_section_flags (abfd, sec,
bfd_sec_flags_from_mmo_flags (flags)
| bfd_get_section_flags (abfd, sec)
| (section_length != 0 ? SEC_HAS_CONTENTS : 0))
|| ! bfd_set_section_size (abfd, sec, sec->size + section_length)
|| (! sec->user_set_vma
&& ! bfd_set_section_vma (abfd, sec, section_vma)))
{
return NULL;
}
loc->next = NULL;
if (mmo_section_data (sec)->tail != NULL)
mmo_section_data (sec)->tail->next = loc;
else
mmo_section_data (sec)->head = loc;
mmo_section_data (sec)->tail = loc;
loc->where = section_vma;
return sec;
format_error_free:
free (secname);
format_error:
if (bfd_seek (abfd, orig_pos, SEEK_SET) != 0)
return NULL;
return mmo_get_generic_spec_data_section (abfd, spec_data_number);
}
static bfd_byte
mmo_get_byte (bfd *abfd)
{
bfd_byte retval;
if (abfd->tdata.mmo_data->byte_no == 0)
{
if (! abfd->tdata.mmo_data->have_error
&& bfd_bread (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
{
abfd->tdata.mmo_data->have_error = TRUE;
return 128;
}
}
retval = abfd->tdata.mmo_data->buf[abfd->tdata.mmo_data->byte_no];
abfd->tdata.mmo_data->byte_no = (abfd->tdata.mmo_data->byte_no + 1) % 4;
return retval;
}
static void
mmo_write_byte (bfd *abfd, bfd_byte value)
{
abfd->tdata.mmo_data->buf[(abfd->tdata.mmo_data->byte_no++ % 4)] = value;
if ((abfd->tdata.mmo_data->byte_no % 4) == 0)
{
if (! abfd->tdata.mmo_data->have_error
&& bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
abfd->tdata.mmo_data->have_error = TRUE;
}
}
static bfd_boolean
mmo_create_symbol (bfd *abfd, const char *symname, bfd_vma addr, enum
mmo_sym_type sym_type, unsigned int serno)
{
struct mmo_symbol *n;
n = (struct mmo_symbol *) bfd_alloc (abfd, sizeof (struct mmo_symbol));
if (n == NULL)
return FALSE;
n->name = bfd_alloc (abfd, strlen (symname) + 1);
if (n->name == NULL)
return FALSE;
strcpy (n->name, symname);
n->value = addr;
n->sym_type = sym_type;
n->serno = serno;
if (abfd->tdata.mmo_data->symbols == NULL)
abfd->tdata.mmo_data->symbols = n;
else
abfd->tdata.mmo_data->symtail->next = n;
abfd->tdata.mmo_data->symtail = n;
n->next = NULL;
++abfd->symcount;
if (strcmp (symname, MMIX_START_SYMBOL_NAME) == 0
&& bfd_get_start_address (abfd) != addr)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: initialization value for $255 is not `Main'\n"),
bfd_get_filename (abfd));
bfd_set_error (bfd_error_bad_value);
return FALSE;
}
return TRUE;
}
static bfd_boolean
mmo_get_symbols (bfd *abfd)
{
bfd_byte m = mmo_get_byte (abfd);
if (abfd->tdata.mmo_data->have_error)
return FALSE;
if (m & MMO3_LEFT)
mmo_get_symbols (abfd);
if (m & MMO3_SYMBITS)
{
bfd_byte c = mmo_get_byte (abfd);
bfd_byte j = m & MMO3_TYPEBITS;
bfd_vma addr = 0;
enum mmo_sym_type sym_type;
unsigned int serno = 0;
bfd_byte k;
if (m & MMO3_WCHAR)
{
bfd_byte c2 = mmo_get_byte (abfd);
if (c != 0)
{
abfd->tdata.mmo_data->lop_stab_symbol
[abfd->tdata.mmo_data->symbol_position] = 0;
(*_bfd_error_handler)
(_("%s: unsupported wide character sequence"
" 0x%02X 0x%02X after symbol name starting with `%s'\n"),
bfd_get_filename (abfd), c, c2,
abfd->tdata.mmo_data->lop_stab_symbol);
bfd_set_error (bfd_error_bad_value);
abfd->tdata.mmo_data->have_error = TRUE;
return FALSE;
}
else
c = c2;
}
abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position++] = c;
abfd->tdata.mmo_data->lop_stab_symbol[abfd->tdata.mmo_data->symbol_position] = 0;
if (j & MMO3_REGQUAL_BITS)
{
if (j == MMO3_REGQUAL_BITS)
{
sym_type = mmo_reg_sym;
addr = mmo_get_byte (abfd);
}
else if (j <= 8)
{
unsigned int i;
for (i = 0; i < j; i++)
addr = (addr << 8) + mmo_get_byte (abfd);
if (addr == 0 && j == MMO3_UNDEF)
sym_type = mmo_undef_sym;
else
sym_type = mmo_abs_sym;
}
else
{
unsigned int i;
for (i = MMO3_DATA; i < j; i++)
addr = (addr << 8) + mmo_get_byte (abfd);
addr += (bfd_vma) 0x20 << 56;
sym_type = mmo_data_sym;
}
do
{
k = mmo_get_byte (abfd);
serno = (serno << 7) + k;
}
while (k < 128);
serno -= 128;
if (! abfd->tdata.mmo_data->have_error
&& ! mmo_create_symbol (abfd,
abfd->tdata.mmo_data->lop_stab_symbol
+ 1,
addr, sym_type, serno))
abfd->tdata.mmo_data->have_error = TRUE;
}
if (m & MMO3_MIDDLE)
mmo_get_symbols (abfd);
abfd->tdata.mmo_data->symbol_position--;
}
if (m & MMO3_RIGHT)
mmo_get_symbols (abfd);
return ! abfd->tdata.mmo_data->have_error;
}
static INLINE bfd_byte *
mmo_get_loc (asection *sec, bfd_vma vma, int size)
{
bfd_size_type allocated_size;
struct mmo_section_data_struct *sdatap = mmo_section_data (sec);
struct mmo_data_list_struct *datap = sdatap->head;
struct mmo_data_list_struct *entry;
for (; datap != NULL; datap = datap->next)
{
if (datap->where <= vma
&& datap->where + datap->size >= vma + size)
return datap->data + vma - datap->where;
else if (datap->where <= vma
&& datap->where + datap->allocated_size >= vma + size
&& (datap->next == NULL || datap->next->where >= vma + size))
{
datap->size += (vma + size) - (datap->where + datap->size);
if (vma + size > sec->vma + sec->size)
sec->size += (vma + size) - (sec->vma + sec->size);
return datap->data + vma - datap->where;
}
}
for (datap = sdatap->head; datap != NULL; datap = datap->next)
if ((datap->where <= vma && datap->where + datap->size > vma)
|| (datap->where < vma + size
&& datap->where + datap->size >= vma + size))
return NULL;
allocated_size
= (size + MMO_SEC_CONTENTS_CHUNK_SIZE - 1) & ~(MMO_SEC_CONTENTS_CHUNK_SIZE - 1);
entry = (mmo_data_list_type *)
bfd_zalloc (sec->owner, sizeof (mmo_data_list_type) + allocated_size);
if (entry == NULL)
return NULL;
entry->where = vma;
entry->size = size;
entry->allocated_size = allocated_size;
datap = sdatap->head;
if (sdatap->tail != NULL && entry->where >= sdatap->tail->where)
{
sdatap->tail->next = entry;
entry->next = NULL;
sdatap->tail = entry;
}
else
{
mmo_data_list_type **look;
for (look = &sdatap->head;
*look != NULL && (*look)->where < entry->where;
look = &(*look)->next)
;
entry->next = *look;
*look = entry;
if (entry->next == NULL)
{
sdatap->tail = entry;
if (! bfd_set_section_flags (sec->owner, sec,
bfd_get_section_flags (sec->owner, sec)
| SEC_HAS_CONTENTS))
return NULL;
}
}
if (vma + size > sec->vma + sec->size)
sec->size += (vma + size) - (sec->vma + sec->size);
return entry->data;
}
static void
mmo_map_set_sizes (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
void *ignored ATTRIBUTE_UNUSED)
{
sec->lma = sec->vma;
}
static bfd_boolean
mmo_scan (bfd *abfd)
{
unsigned int i;
unsigned int lineno = 1;
bfd_boolean error = FALSE;
bfd_vma vma = 0;
asection *sec = bfd_make_section_old_way (abfd, MMO_TEXT_SECTION_NAME);
asection *non_spec_sec = NULL;
bfd_vma non_spec_vma = 0;
char *current_filename = NULL;
bfd_size_type nbytes_read = 0;
bfd_byte buf[8];
long stab_loc = -1;
char *file_names[256];
memset (file_names, 0, sizeof (file_names));
if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
goto error_return;
while ((nbytes_read = bfd_bread (buf, 4, abfd)) == 4)
{
if (buf[0] == LOP)
{
unsigned int y = bfd_get_8 (abfd, buf + 2);
unsigned int z = bfd_get_8 (abfd, buf + 3);
if ((buf[1] != LOP_QUOTE || y != 0 || z != 1)
&& non_spec_sec != NULL)
{
sec = non_spec_sec;
vma = non_spec_vma;
non_spec_sec = NULL;
}
switch (buf[1])
{
default:
(*_bfd_error_handler)
(_("%s: invalid mmo file: unsupported lopcode `%d'\n"),
bfd_get_filename (abfd), buf[1]);
bfd_set_error (bfd_error_bad_value);
goto error_return;
case LOP_QUOTE:
if (y != 0 || z != 1)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: expected YZ = 1 got YZ = %d for lop_quote\n"),
bfd_get_filename (abfd), y*256+z);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
if (bfd_bread (buf, 4, abfd) != 4)
goto error_return;
mmo_xore_32 (sec, vma, bfd_get_32 (abfd, buf));
vma += 4;
vma &= ~3;
lineno++;
break;
case LOP_LOC:
vma = (bfd_vma) y << 56;
if (z == 1)
{
if (bfd_bread (buf, 4, abfd) != 4)
goto error_return;
vma += bfd_get_32 (abfd, buf);
}
else if (z == 2)
{
if (bfd_bread (buf, 8, abfd) != 8)
goto error_return;
vma += bfd_get_64 (abfd, buf);
}
else
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_loc\n"),
bfd_get_filename (abfd), z);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
sec = mmo_decide_section (abfd, vma);
if (sec == NULL)
goto error_return;
break;
case LOP_SKIP:
vma += y * 256 + z;
sec = mmo_decide_section (abfd, vma);
if (sec == NULL)
goto error_return;
break;
case LOP_FIXO:
{
bfd_vma p = (bfd_vma) y << 56;
asection *fixosec;
if (z == 1)
{
if (bfd_bread (buf, 4, abfd) != 4)
goto error_return;
p += bfd_get_32 (abfd, buf);
}
else if (z == 2)
{
if (bfd_bread (buf, 8, abfd) != 8)
goto error_return;
p += bfd_get_64 (abfd, buf);
}
else
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: expected z = 1 or z = 2, got z = %d for lop_fixo\n"),
bfd_get_filename (abfd), z);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
fixosec = mmo_decide_section (abfd, p);
if (fixosec == NULL)
goto error_return;
mmo_xore_64 (fixosec, p, vma);
}
break;
case LOP_FIXR:
{
unsigned int yz = (y * 256 + z);
bfd_vma p = vma + 2 - 4 * yz;
asection *fixrsec = mmo_decide_section (abfd, p);
if (fixrsec == NULL)
goto error_return;
mmo_xore_16 (fixrsec, p, yz);
}
break;
case LOP_FIXRX:
{
bfd_vma delta;
bfd_vma p;
asection *fixrsec;
if (y != 0)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: expected y = 0, got y = %d for lop_fixrx\n"),
bfd_get_filename (abfd), y);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
if (z != 16 && z != 24)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: expected z = 16 or z = 24, got z = %d for lop_fixrx\n"),
bfd_get_filename (abfd), z);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
if (bfd_bread (buf, 4, abfd) != 4)
goto error_return;
delta = bfd_get_32 (abfd, buf);
if (buf[0] == 0)
p = vma - 4 * delta;
else if (buf[0] == 1)
p = vma - 4 * ((delta & 0xffffff) - (1 << z));
else
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: leading byte of operand word must be 0 or 1, got %d for lop_fixrx\n"),
bfd_get_filename (abfd), buf[0]);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
fixrsec = mmo_decide_section (abfd, vma);
if (fixrsec == NULL)
goto error_return;
mmo_xore_32 (fixrsec, p, delta);
}
break;
case LOP_FILE:
if (z != 0)
{
char *fname = bfd_malloc (z * 4 + 1);
if (fname == NULL)
{
(*_bfd_error_handler)
(_("%s: cannot allocate file name for file number %d, %d bytes\n"),
bfd_get_filename (abfd), y, z * 4 + 1);
bfd_set_error (bfd_error_system_call);
goto error_return;
}
fname[z * 4] = 0;
for (i = 0; i < z; i++)
{
if (bfd_bread (fname + i * 4, 4, abfd) != 4)
{
free (fname);
goto error_return;
}
}
if (file_names[y] != NULL)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: file number %d `%s',"
" was already entered as `%s'\n"),
bfd_get_filename (abfd), y, fname, file_names[y]);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
file_names[y] = fname;
}
if (file_names[y] == NULL)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: file name for number %d"
" was not specified before use\n"),
bfd_get_filename (abfd), y);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
current_filename = file_names[y];
lineno = 0;
break;
case LOP_LINE:
lineno = y * 256 + z;
break;
case LOP_SPEC:
non_spec_sec = sec;
non_spec_vma = vma;
sec = mmo_get_spec_section (abfd, y * 256 + z);
if (sec == NULL)
goto error_return;
vma = sec->vma;
break;
case LOP_PRE:
{
if (z >= 1
&& bfd_bread (abfd->tdata.mmo_data->created, 4,
abfd) != 4)
goto error_return;
for (i = 1; i < z; i++)
if (bfd_bread (buf, 4, abfd) != 4)
goto error_return;
}
break;
case LOP_POST:
{
asection *rsec;
bfd_byte *loc;
bfd_vma first_octa;
bfd_vma startaddr_octa;
if (bfd_bread (buf, 8, abfd) != 8)
goto error_return;
first_octa = bfd_get_64 (abfd, buf);
if (z != 255)
{
rsec
= bfd_make_section_old_way (abfd,
MMIX_REG_CONTENTS_SECTION_NAME);
rsec->vma = z * 8;
loc = mmo_get_loc (rsec, z * 8, (255 - z) * 8);
bfd_put_64 (abfd, first_octa, loc);
for (i = z + 1; i < 255; i++)
{
if (bfd_bread (loc + (i - z) * 8, 8, abfd) != 8)
goto error_return;
}
if (bfd_bread (buf, 8, abfd) != 8)
goto error_return;
startaddr_octa = bfd_get_64 (abfd, buf);
}
else
startaddr_octa = first_octa;
if (! bfd_set_start_address (abfd, startaddr_octa))
{
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
}
break;
case LOP_STAB:
if (y != 0 || z != 0)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: fields y and z of lop_stab"
" non-zero, y: %d, z: %d\n"),
bfd_get_filename (abfd), y, z);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
stab_loc = bfd_tell (abfd);
if (abfd->tdata.mmo_data->max_symbol_length != 0
&& ! mmo_get_symbols (abfd))
goto error_return;
break;
case LOP_END:
{
struct stat statbuf;
long curpos = bfd_tell (abfd);
if (bfd_stat (abfd, &statbuf) < 0)
goto error_return;
if (statbuf.st_size != curpos)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: lop_end not last item in"
" file\n"),
bfd_get_filename (abfd));
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
if ((long) (y * 256 + z) * 4 != (curpos - stab_loc) - 4)
{
(*_bfd_error_handler)
(_("%s: invalid mmo file: YZ of lop_end (%ld)"
" not equal to the number of tetras to the preceding"
" lop_stab (%ld)\n"),
bfd_get_filename (abfd), (long) (y * 256 + z),
(curpos - stab_loc - 4)/4);
bfd_set_error (bfd_error_bad_value);
goto error_return;
}
bfd_map_over_sections (abfd, mmo_map_set_sizes, NULL);
goto done;
}
}
}
else
{
mmo_xore_32 (sec, vma & ~3, bfd_get_32 (abfd, buf));
vma += 4;
vma &= ~3;
lineno++;
}
}
if (nbytes_read != 0)
bfd_set_error (bfd_error_system_call);
else
bfd_set_error (bfd_error_bad_value);
error_return:
error = TRUE;
done:
sec = bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
if (sec != NULL
&& (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
&& ! bfd_set_section_flags (abfd, sec,
bfd_get_section_flags (abfd, sec)
| SEC_ALLOC | SEC_LOAD | SEC_CODE))
error = TRUE;
sec = bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
if (sec != NULL
&& (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
&& ! bfd_set_section_flags (abfd, sec,
bfd_get_section_flags (abfd, sec)
| SEC_ALLOC | SEC_LOAD))
error = TRUE;
for (i = 0; i < sizeof (file_names) / sizeof (file_names[0]); i++)
if (file_names[i])
free (file_names[i]);
return ! error;
}
static bfd_boolean
mmo_new_section_hook (bfd *abfd ATTRIBUTE_UNUSED, asection *newsect)
{
newsect->used_by_bfd =
bfd_zalloc (abfd, sizeof (struct mmo_section_data_struct));
if (!newsect->used_by_bfd)
return FALSE;
newsect->alignment_power = 2;
return TRUE;
}
static bfd_boolean
mmo_get_section_contents (bfd *abfd ATTRIBUTE_UNUSED,
asection *sec,
void * location,
file_ptr offset,
bfd_size_type bytes_to_do)
{
while (bytes_to_do)
{
int chunk_size
= (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
bfd_byte *loc;
do
loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
while (loc == NULL && (chunk_size /= 2) != 0);
if (chunk_size == 0)
return FALSE;
memcpy (location, loc, chunk_size);
location += chunk_size;
bytes_to_do -= chunk_size;
offset += chunk_size;
}
return TRUE;
}
static long
mmo_get_symtab_upper_bound (bfd *abfd)
{
return (abfd->symcount + 1) * sizeof (asymbol *);
}
static int
mmo_sort_mmo_symbols (const void *arg1, const void *arg2)
{
const struct mmo_symbol *sym1 = *(const struct mmo_symbol **) arg1;
const struct mmo_symbol *sym2 = *(const struct mmo_symbol **) arg2;
if (sym1->serno < sym2->serno)
return -1;
else if (sym1->serno > sym2->serno)
return 1;
return ((const char *) arg1 - (const char *) arg2);
}
static long
mmo_canonicalize_symtab (bfd *abfd, asymbol **alocation)
{
unsigned int symcount = bfd_get_symcount (abfd);
asymbol *csymbols;
unsigned int i;
csymbols = abfd->tdata.mmo_data->csymbols;
if (csymbols == NULL)
{
asymbol *c;
struct mmo_symbol *s;
struct mmo_symbol **msp;
for (s = abfd->tdata.mmo_data->symbols,
msp = (struct mmo_symbol **) alocation;
s != NULL;
s = s->next, ++msp)
*msp = s;
*msp = NULL;
qsort (alocation, symcount, sizeof (struct mmo_symbol *),
mmo_sort_mmo_symbols);
csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
if (csymbols == NULL && symcount != 0)
return FALSE;
abfd->tdata.mmo_data->csymbols = csymbols;
for (msp = (struct mmo_symbol **) alocation, c = csymbols;
*msp != NULL;
msp++, ++c)
{
s = *msp;
c->the_bfd = abfd;
c->name = s->name;
c->value = s->value;
c->flags = BSF_GLOBAL;
if (s->sym_type == mmo_data_sym)
{
c->section
= bfd_get_section_by_name (abfd, MMO_DATA_SECTION_NAME);
if (c->section == NULL)
c->section = bfd_abs_section_ptr;
else
c->value -= c->section->vma;
}
else if (s->sym_type == mmo_undef_sym)
c->section = bfd_und_section_ptr;
else if (s->sym_type == mmo_reg_sym)
{
c->section
= bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
}
else
{
asection *textsec
= bfd_get_section_by_name (abfd, MMO_TEXT_SECTION_NAME);
asection *datasec;
if (textsec != NULL
&& c->value >= textsec->vma
&& c->value <= textsec->vma + textsec->size)
{
c->section = textsec;
c->value -= c->section->vma;
}
else if ((datasec
= bfd_get_section_by_name (abfd,
MMO_DATA_SECTION_NAME))
!= NULL
&& c->value >= datasec->vma
&& c->value <= datasec->vma + datasec->size)
{
c->section = datasec;
c->value -= c->section->vma;
}
else
c->section = bfd_abs_section_ptr;
}
c->udata.p = NULL;
}
}
for (i = 0; i < symcount; i++)
*alocation++ = csymbols++;
*alocation = NULL;
return symcount;
}
static void
mmo_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
asymbol *symbol, symbol_info *ret)
{
bfd_symbol_info (symbol, ret);
}
static void
mmo_print_symbol (bfd *abfd, void *afile, asymbol *symbol,
bfd_print_symbol_type how)
{
FILE *file = (FILE *) afile;
switch (how)
{
case bfd_print_symbol_name:
fprintf (file, "%s", symbol->name);
break;
default:
bfd_print_symbol_vandf (abfd, file, symbol);
fprintf (file, " %-5s %s",
symbol->section->name,
symbol->name);
}
}
static int
mmo_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
bfd_boolean exec ATTRIBUTE_UNUSED)
{
return 0;
}
static bfd_boolean
mmo_internal_write_header (bfd *abfd)
{
const char lop_pre_bfd[] = { LOP, LOP_PRE, 1, 1};
if (bfd_bwrite (lop_pre_bfd, 4, abfd) != 4)
return FALSE;
if (bfd_bwrite (abfd->tdata.mmo_data->created, 4, abfd) != 4)
return FALSE;
return TRUE;
}
static bfd_boolean
mmo_internal_write_post (bfd *abfd, int z, asection *sec)
{
int i;
bfd_byte buf[8];
mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_POST << 16) | z);
for (i = z; i < 255; i++)
{
bfd_byte *data = mmo_get_loc (sec, i * 8, 8);
if (bfd_bwrite (data, 8, abfd) != 8)
return FALSE;
}
bfd_put_64 (abfd, bfd_get_start_address (abfd), buf);
return ! abfd->tdata.mmo_data->have_error && bfd_bwrite (buf, 8, abfd) == 8;
}
static flagword
mmo_sec_flags_from_bfd_flags (flagword flags)
{
flagword oflags = 0;
if (flags & SEC_ALLOC)
oflags |= MMO_SEC_ALLOC;
if (flags & SEC_LOAD)
oflags |= MMO_SEC_LOAD;
if (flags & SEC_RELOC)
oflags |= MMO_SEC_RELOC;
if (flags & SEC_READONLY)
oflags |= MMO_SEC_READONLY;
if (flags & SEC_CODE)
oflags |= MMO_SEC_CODE;
if (flags & SEC_DATA)
oflags |= MMO_SEC_DATA;
if (flags & SEC_NEVER_LOAD)
oflags |= MMO_SEC_NEVER_LOAD;
if (flags & SEC_IS_COMMON)
oflags |= MMO_SEC_IS_COMMON;
if (flags & SEC_DEBUGGING)
oflags |= MMO_SEC_DEBUGGING;
return oflags;
}
static flagword
bfd_sec_flags_from_mmo_flags (flagword flags)
{
flagword oflags = 0;
if (flags & MMO_SEC_ALLOC)
oflags |= SEC_ALLOC;
if (flags & MMO_SEC_LOAD)
oflags |= SEC_LOAD;
if (flags & MMO_SEC_RELOC)
oflags |= SEC_RELOC;
if (flags & MMO_SEC_READONLY)
oflags |= SEC_READONLY;
if (flags & MMO_SEC_CODE)
oflags |= SEC_CODE;
if (flags & MMO_SEC_DATA)
oflags |= SEC_DATA;
if (flags & MMO_SEC_NEVER_LOAD)
oflags |= SEC_NEVER_LOAD;
if (flags & MMO_SEC_IS_COMMON)
oflags |= SEC_IS_COMMON;
if (flags & MMO_SEC_DEBUGGING)
oflags |= SEC_DEBUGGING;
return oflags;
}
static bfd_boolean
mmo_has_leading_or_trailing_zero_tetra_p (bfd *abfd, asection *sec)
{
bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
if (sec->size < 4)
return FALSE;
if (bfd_get_32 (abfd, mmo_get_loc (sec, secaddr, 4)) == 0
&& bfd_get_32 (abfd,
mmo_get_loc (sec, secaddr + sec->size - 4, 4)) == 0)
return TRUE;
return FALSE;
}
static bfd_boolean
mmo_internal_write_section (bfd *abfd, asection *sec)
{
if (strcmp (sec->name, MMO_TEXT_SECTION_NAME) == 0)
{
bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
if (sec->size != 0
&& (secaddr + sec->size >= (bfd_vma) 1 << 56
|| (secaddr & 3) != 0
|| (sec->size & 3) != 0
|| mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
{
if (!mmo_write_section_description (abfd, sec))
return FALSE;
}
return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
}
else if (strcmp (sec->name, MMO_DATA_SECTION_NAME) == 0)
{
bfd_vma secaddr = bfd_get_section_vma (abfd, sec);
if (sec->size != 0
&& (secaddr < (bfd_vma) 0x20 << 56
|| secaddr + sec->size >= (bfd_vma) 0x21 << 56
|| (secaddr & 3) != 0
|| (sec->size & 3) != 0
|| mmo_has_leading_or_trailing_zero_tetra_p (abfd, sec)))
{
if (!mmo_write_section_description (abfd, sec))
return FALSE;
}
return mmo_write_loc_chunk_list (abfd, mmo_section_data (sec)->head);
}
else if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
{
bfd_set_error (bfd_error_bad_value);
return FALSE;
}
else if (strncmp (sec->name, MMIX_OTHER_SPEC_SECTION_PREFIX,
strlen (MMIX_OTHER_SPEC_SECTION_PREFIX)) == 0)
{
int n = atoi (sec->name + strlen (MMIX_OTHER_SPEC_SECTION_PREFIX));
mmo_write_tetra_raw (abfd, (LOP << 24) | (LOP_SPEC << 16) | n);
return (! abfd->tdata.mmo_data->have_error
&& mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
}
else if ((bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS) != 0
&& sec->size != 0)
{
if (!mmo_write_section_description (abfd, sec))
return FALSE;
if (bfd_get_section_flags (abfd, sec) & SEC_LOAD)
return (! abfd->tdata.mmo_data->have_error
&& mmo_write_loc_chunk_list (abfd,
mmo_section_data (sec)->head));
return (! abfd->tdata.mmo_data->have_error
&& mmo_write_chunk_list (abfd, mmo_section_data (sec)->head));
}
return TRUE;
}
static bfd_boolean
mmo_write_section_description (bfd *abfd, asection *sec)
{
mmo_write_tetra_raw (abfd, LOP_SPEC_SECTION);
mmo_write_tetra (abfd, (strlen (sec->name) + 3) / 4);
mmo_write_chunk (abfd, (bfd_byte *) sec->name, strlen (sec->name));
mmo_flush_chunk (abfd);
mmo_write_tetra (abfd,
mmo_sec_flags_from_bfd_flags
(bfd_get_section_flags (abfd, sec)));
mmo_write_octa (abfd, sec->size);
mmo_write_octa (abfd, bfd_get_section_vma (abfd, sec));
return TRUE;
}
static bfd_boolean
mmo_set_section_contents (bfd *abfd ATTRIBUTE_UNUSED, sec_ptr sec,
const void *location, file_ptr offset,
bfd_size_type bytes_to_do)
{
while (bytes_to_do)
{
int chunk_size
= (int) bytes_to_do != 0 ? bytes_to_do : MMO_SEC_CONTENTS_CHUNK_SIZE;
bfd_byte *loc;
do
loc = mmo_get_loc (sec, sec->vma + offset, chunk_size);
while (loc == NULL && (chunk_size /= 2) != 0);
if (chunk_size == 0)
return FALSE;
memcpy (loc, location, chunk_size);
location += chunk_size;
bytes_to_do -= chunk_size;
offset += chunk_size;
}
return TRUE;
}
static bfd_boolean
mmo_internal_add_3_sym (bfd *abfd, struct mmo_symbol_trie *rootp,
const struct mmo_symbol *symp)
{
const char *name = symp->name;
struct mmo_symbol_trie *trie = rootp;
struct mmo_symbol_trie **triep = NULL;
while (*name && trie != NULL)
{
if (*name < trie->symchar)
{
triep = &trie->left;
trie = trie->left;
}
else if (*name > trie->symchar)
{
triep = &trie->right;
trie = trie->right;
}
else if (*name == trie->symchar)
{
triep = &trie->middle;
name++;
if (*name)
trie = trie->middle;
}
}
while (*name != 0)
{
trie = bfd_zalloc (abfd, sizeof (struct mmo_symbol_trie));
*triep = trie;
trie->symchar = *name++;
triep = &trie->middle;
}
if (trie->sym.name != NULL)
{
(*_bfd_error_handler)
(_("%s: invalid symbol table: duplicate symbol `%s'\n"),
bfd_get_filename (abfd), trie->sym.name);
bfd_set_error (bfd_error_bad_value);
return FALSE;
}
memcpy (&trie->sym, symp, sizeof *symp);
return TRUE;
}
static unsigned int
mmo_internal_3_length (bfd *abfd, struct mmo_symbol_trie *trie)
{
unsigned int length = 1;
if (trie == NULL)
return 0;
length += mmo_internal_3_length (abfd, trie->left);
length += 1 + mmo_internal_3_length (abfd, trie->middle);
length += mmo_internal_3_length (abfd, trie->right);
if (trie->sym.name != NULL)
{
unsigned int serno = trie->sym.serno;
if (trie->sym.sym_type == mmo_reg_sym)
length++;
else if (trie->sym.sym_type == mmo_undef_sym)
length += 2;
else
{
bfd_vma value = trie->sym.value;
if (trie->sym.sym_type == mmo_data_sym)
value -= (bfd_vma) 0x20 << 56;
do
{
value >>= 8;
length++;
}
while (value != 0);
}
do
{
serno >>= 7;
length++;
}
while (serno != 0);
}
return length;
}
static void
mmo_beb128_out (bfd *abfd, int serno, int marker)
{
if (serno & ~0x7f)
mmo_beb128_out (abfd, serno >> 7, 0);
mmo_write_byte (abfd, marker | (serno & 0x7f));
}
static void
mmo_internal_3_dump (bfd *abfd, struct mmo_symbol_trie *trie)
{
bfd_byte control = 0;
if (trie == NULL)
return;
if (trie->left)
control |= MMO3_LEFT;
if (trie->middle)
control |= MMO3_MIDDLE;
if (trie->right)
control |= MMO3_RIGHT;
if (trie->sym.name != NULL)
{
if (trie->sym.sym_type == mmo_reg_sym)
control |= MMO3_REGQUAL_BITS;
else if (trie->sym.sym_type == mmo_undef_sym)
control |= MMO3_UNDEF;
else
{
bfd_vma value = trie->sym.value;
if (trie->sym.sym_type == mmo_data_sym)
{
control |= MMO3_DATA;
value -= (bfd_vma) 0x20 << 56;
}
do
{
value >>= 8;
control++;
}
while (value != 0);
}
}
mmo_write_byte (abfd, control);
mmo_internal_3_dump (abfd, trie->left);
if (control & MMO3_SYMBITS)
{
mmo_write_byte (abfd, trie->symchar);
if (trie->sym.name != NULL)
{
if (trie->sym.sym_type == mmo_reg_sym)
mmo_write_byte (abfd, trie->sym.value);
else if (trie->sym.sym_type == mmo_undef_sym)
{
mmo_write_byte (abfd, 0);
mmo_write_byte (abfd, 0);
}
else
{
bfd_vma value = trie->sym.value;
bfd_byte byte_n = control & 15;
if (trie->sym.sym_type == mmo_data_sym)
{
value -= (bfd_vma) 0x20 << 56;
byte_n -= 8;
}
do
{
mmo_write_byte (abfd, (value >> ((byte_n - 1) * 8)) & 0xff);
byte_n--;
}
while (byte_n != 0);
}
mmo_beb128_out (abfd, trie->sym.serno, 128);
}
mmo_internal_3_dump (abfd, trie->middle);
}
mmo_internal_3_dump (abfd, trie->right);
}
static bfd_boolean
mmo_write_symbols_and_terminator (bfd *abfd)
{
int count = bfd_get_symcount (abfd);
asymbol *maintable[2];
asymbol **table;
asymbol **orig_table = bfd_get_outsymbols (abfd);
int serno;
struct mmo_symbol_trie root;
int trie_len;
int i;
bfd_byte buf[4];
asymbol *fakemain = bfd_make_empty_symbol (abfd);
fakemain->flags = BSF_GLOBAL;
fakemain->value = bfd_get_start_address (abfd);
fakemain->name = MMIX_START_SYMBOL_NAME;
fakemain->section = bfd_abs_section_ptr;
maintable[0] = fakemain;
maintable[1] = NULL;
memset (&root, 0, sizeof (root));
root.symchar = 0xff;
table = bfd_alloc (abfd, (count + 1) * sizeof (asymbol *));
if (table == NULL)
return FALSE;
memcpy (table, orig_table, count * sizeof (asymbol *));
for (i = 0; i < count; i++)
if (table[i] != NULL
&& strcmp (table[i]->name, MMIX_START_SYMBOL_NAME) == 0
&& (table[i]->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL)
{
asymbol *mainsym = table[i];
memcpy (table + 1, orig_table, i * sizeof (asymbol *));
table[0] = mainsym;
if ((mainsym->value
+ mainsym->section->output_section->vma
+ mainsym->section->output_offset)
!= bfd_get_start_address (abfd))
{
char vmas_main[40];
char vmas_start[40];
bfd_vma vma_start = bfd_get_start_address (abfd);
sprintf_vma (vmas_main, mainsym->value);
sprintf_vma (vmas_start, vma_start);
(*_bfd_error_handler)
(_("%s: Bad symbol definition: `Main' set to %s rather"
" than the start address %s\n"),
bfd_get_filename (abfd), vmas_main, vmas_start);
bfd_set_error (bfd_error_bad_value);
return FALSE;
}
break;
}
if (i == count && count != 0)
{
memcpy (table + 1, orig_table, count * sizeof (asymbol *));
table[0] = fakemain;
count++;
}
for (i = 0, serno = 1; i < count && table[i] != NULL; i++)
{
asymbol *s = table[i];
if ((s->flags & (BSF_DEBUGGING|BSF_GLOBAL)) == BSF_GLOBAL
&& strspn (s->name,
valid_mmo_symbol_character_set) == strlen (s->name))
{
struct mmo_symbol sym;
memset (&sym, 0, sizeof (sym));
sym.name = (char *) s->name;
sym.value =
s->value
+ s->section->output_section->vma
+ s->section->output_offset;
if (bfd_is_und_section (s->section))
sym.sym_type = mmo_undef_sym;
else if (strcmp (s->section->name, MMO_DATA_SECTION_NAME) == 0
&& (sym.value >> 48) == 0x2000)
sym.sym_type = mmo_data_sym;
else if (strcmp (s->section->name, MMIX_REG_SECTION_NAME) == 0)
sym.sym_type = mmo_reg_sym;
else if (strcmp (s->section->name,
MMIX_REG_CONTENTS_SECTION_NAME) == 0)
{
sym.sym_type = mmo_reg_sym;
sym.value /= 8;
}
else
sym.sym_type = mmo_abs_sym;
sym.serno = serno++;
if (! mmo_internal_add_3_sym (abfd, &root, &sym))
return FALSE;
}
}
root.symchar = ':';
root.middle = root.left;
root.right = NULL;
root.left = NULL;
trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
if (trie_len > 0xffff)
{
struct mmo_symbol sym;
(*_bfd_error_handler)
(_("%s: warning: symbol table too large for mmo, larger than 65535"
" 32-bit words: %d. Only `Main' will be emitted.\n"),
bfd_get_filename (abfd), trie_len);
memset (&sym, 0, sizeof (sym));
sym.sym_type = mmo_abs_sym;
sym.name = MMIX_START_SYMBOL_NAME;
sym.serno = 1;
sym.value = bfd_get_start_address (abfd);
memset (&root, 0, sizeof (root));
root.left = root.middle;
root.symchar = 0xff;
root.middle = NULL;
root.right = NULL;
if (! mmo_internal_add_3_sym (abfd, &root, &sym))
return FALSE;
root.symchar = ':';
root.middle = root.left;
root.right = NULL;
root.left = NULL;
trie_len = (mmo_internal_3_length (abfd, &root) + 3)/4;
}
abfd->tdata.mmo_data->byte_no = 0;
bfd_put_32 (abfd, (LOP << 24) | (LOP_STAB << 16), buf);
if (bfd_bwrite (buf, 4, abfd) != 4)
return FALSE;
mmo_internal_3_dump (abfd, &root);
if (trie_len != (abfd->tdata.mmo_data->byte_no + 3)/4)
{
(*_bfd_error_handler)
(_("%s: internal error, symbol table changed size from %d to %d"
" words\n"),
bfd_get_filename (abfd), trie_len,
(abfd->tdata.mmo_data->byte_no + 3)/4);
bfd_set_error (bfd_error_bad_value);
return FALSE;
}
if ((abfd->tdata.mmo_data->byte_no % 4) != 0
|| abfd->tdata.mmo_data->have_error)
{
memset (abfd->tdata.mmo_data->buf + (abfd->tdata.mmo_data->byte_no % 4),
0, 4 - (abfd->tdata.mmo_data->byte_no % 4));
if (abfd->tdata.mmo_data->have_error
|| bfd_bwrite (abfd->tdata.mmo_data->buf, 4, abfd) != 4)
return FALSE;
}
bfd_put_32 (abfd, (LOP << 24) | (LOP_END << 16) | trie_len, buf);
return bfd_bwrite (buf, 4, abfd) == 4;
}
static void
mmo_write_section_unless_reg_contents (bfd *abfd, asection *sec, void *p)
{
struct mmo_write_sec_info *infop = (struct mmo_write_sec_info *) p;
if (! infop->retval)
return;
if (strcmp (sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
{
infop->reg_section = sec;
return;
}
if (strcmp (sec->name, MMIX_REG_SECTION_NAME) == 0)
{
if (bfd_get_section_flags (abfd, sec) & SEC_HAS_CONTENTS)
{
(*_bfd_error_handler)
(_("%s: internal error, internal register section %s had"
" contents\n"),
bfd_get_filename (abfd), sec->name);
bfd_set_error (bfd_error_bad_value);
infop->retval = FALSE;
return;
}
return;
}
infop->retval = mmo_internal_write_section (abfd, sec);
}
static bfd_boolean
mmo_write_object_contents (bfd *abfd)
{
struct mmo_write_sec_info wsecinfo;
if (! mmo_internal_write_header (abfd))
return FALSE;
wsecinfo.reg_section = NULL;
wsecinfo.retval = TRUE;
bfd_map_over_sections (abfd, mmo_write_section_unless_reg_contents,
&wsecinfo);
if (! wsecinfo.retval)
return FALSE;
if (wsecinfo.reg_section != NULL)
{
asection *sec = wsecinfo.reg_section;
unsigned int z = (unsigned int) (sec->vma / 8);
if (z < 32 || z >= 255 || (sec->vma & 7) != 0
|| sec->vma != 256 * 8 - sec->size - 8)
{
bfd_set_error (bfd_error_bad_value);
if (sec->size == 0)
(*_bfd_error_handler)
(_("%s: no initialized registers; section length 0\n"),
bfd_get_filename (abfd));
else if (sec->vma > (256 - 32) * 8)
(*_bfd_error_handler)
(_("%s: too many initialized registers; section length %ld\n"),
bfd_get_filename (abfd),
(long) sec->size);
else
(*_bfd_error_handler)
(_("%s: invalid start address for initialized registers of"
" length %ld: 0x%lx%08lx\n"),
bfd_get_filename (abfd),
(long) sec->size,
(unsigned long) (sec->vma >> 32), (unsigned long) (sec->vma));
return FALSE;
}
if (! mmo_internal_write_post (abfd, z, sec))
return FALSE;
}
else
if (! mmo_internal_write_post (abfd, 255, NULL))
return FALSE;
return mmo_write_symbols_and_terminator (abfd);
}
static long
mmo_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED,
asection *sec ATTRIBUTE_UNUSED)
{
return sizeof (void *);
}
long
mmo_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED,
sec_ptr section ATTRIBUTE_UNUSED, arelent **relptr,
asymbol **symbols ATTRIBUTE_UNUSED)
{
*relptr = NULL;
return 0;
}
#define mmo_close_and_cleanup _bfd_generic_close_and_cleanup
#define mmo_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
#define mmo_bfd_is_local_label_name bfd_generic_is_local_label_name
#define mmo_bfd_is_target_special_symbol \
((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
#define mmo_get_lineno _bfd_nosymbols_get_lineno
#define mmo_find_nearest_line _bfd_nosymbols_find_nearest_line
#define mmo_find_inliner_info _bfd_nosymbols_find_inliner_info
#define mmo_make_empty_symbol _bfd_generic_make_empty_symbol
#define mmo_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
#define mmo_read_minisymbols _bfd_generic_read_minisymbols
#define mmo_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
#define mmo_get_section_contents_in_window \
_bfd_generic_get_section_contents_in_window
#define mmo_get_section_contents_in_window_with_mode \
_bfd_generic_get_section_contents_in_window_with_mode
#define mmo_bfd_get_relocated_section_contents \
bfd_generic_get_relocated_section_contents
#define mmo_bfd_gc_sections bfd_generic_gc_sections
#define mmo_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
#define mmo_bfd_link_hash_table_free _bfd_generic_link_hash_table_free
#define mmo_bfd_link_add_symbols _bfd_generic_link_add_symbols
#define mmo_bfd_link_just_syms _bfd_generic_link_just_syms
#define mmo_bfd_final_link _bfd_generic_final_link
#define mmo_bfd_link_split_section _bfd_generic_link_split_section
#define mmo_set_arch_mach bfd_default_set_arch_mach
#define mmo_bfd_relax_section bfd_generic_relax_section
#define mmo_bfd_merge_sections bfd_generic_merge_sections
#define mmo_bfd_is_group_section bfd_generic_is_group_section
#define mmo_bfd_discard_group bfd_generic_discard_group
#define mmo_section_already_linked \
_bfd_generic_section_already_linked
#define mmo_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
#define mmo_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
#define mmo_bfd_copy_private_section_data _bfd_generic_bfd_copy_private_section_data
#define mmo_bfd_copy_private_symbol_data _bfd_generic_bfd_copy_private_symbol_data
#define mmo_bfd_copy_private_header_data _bfd_generic_bfd_copy_private_header_data
#define mmo_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
#define mmo_bfd_print_private_bfd_data _bfd_generic_bfd_print_private_bfd_data
const bfd_target bfd_mmo_vec =
{
"mmo",
bfd_target_mmo_flavour,
BFD_ENDIAN_BIG,
BFD_ENDIAN_BIG,
(HAS_RELOC | EXEC_P |
HAS_LINENO | HAS_DEBUG |
HAS_SYMS | HAS_LOCALS | WP_TEXT),
(SEC_CODE | SEC_DATA | SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD
| SEC_READONLY | SEC_EXCLUDE | SEC_DEBUGGING | SEC_IN_MEMORY),
0,
' ',
16,
bfd_getb64, bfd_getb_signed_64, bfd_putb64,
bfd_getb32, bfd_getb_signed_32, bfd_putb32,
bfd_getb16, bfd_getb_signed_16, bfd_putb16,
bfd_getb64, bfd_getb_signed_64, bfd_putb64,
bfd_getb32, bfd_getb_signed_32, bfd_putb32,
bfd_getb16, bfd_getb_signed_16, bfd_putb16,
{
_bfd_dummy_target,
mmo_object_p,
_bfd_dummy_target,
_bfd_dummy_target,
},
{
bfd_false,
mmo_mkobject,
bfd_false,
bfd_false,
},
{
bfd_false,
mmo_write_object_contents,
bfd_false,
bfd_false,
},
BFD_JUMP_TABLE_GENERIC (mmo),
BFD_JUMP_TABLE_COPY (mmo),
BFD_JUMP_TABLE_CORE (_bfd_nocore),
BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
BFD_JUMP_TABLE_SYMBOLS (mmo),
BFD_JUMP_TABLE_RELOCS (mmo),
BFD_JUMP_TABLE_WRITE (mmo),
BFD_JUMP_TABLE_LINK (mmo),
BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
NULL,
NULL
};