#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
#include "coff/mcore.h"
#include "coff/internal.h"
#include "coff/pe.h"
#include "libcoff.h"
#ifdef BADMAG
#undef BADMAG
#endif
#define BADMAG(x) MCOREBADMAG(x)
#ifndef NUM_ELEM
#define NUM_ELEM(A) (sizeof (A) / sizeof (A)[0])
#endif
extern bfd_boolean mcore_bfd_coff_final_link
PARAMS ((bfd *, struct bfd_link_info *));
static bfd_reloc_status_type mcore_coff_unsupported_reloc
PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
static bfd_boolean coff_mcore_relocate_section
PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
struct internal_reloc *, struct internal_syment *, asection **));
static reloc_howto_type *mcore_coff_reloc_type_lookup
PARAMS ((bfd *, bfd_reloc_code_real_type));
static reloc_howto_type *coff_mcore_rtype_to_howto
PARAMS ((bfd *, asection *, struct internal_reloc *,
struct coff_link_hash_entry *, struct internal_syment *,
bfd_vma *));
static void mcore_emit_base_file_entry
PARAMS ((struct bfd_link_info *, bfd *, asection *, bfd_vma));
static bfd_boolean in_reloc_p PARAMS ((bfd *, reloc_howto_type *));
#define TOC_LOAD_ADJUSTMENT (-32768)
#define TOC_SECTION_NAME ".private.toc"
#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
#define MINUS_ONE (((bfd_vma)0) - 1)
static reloc_howto_type mcore_coff_howto_table[] =
{
HOWTO (IMAGE_REL_MCORE_ABSOLUTE,
0,
0,
0,
FALSE,
0,
complain_overflow_dont,
NULL,
"ABSOLUTE",
FALSE,
0x00,
0x00,
FALSE),
HOWTO (IMAGE_REL_MCORE_ADDR32,
0,
2,
32,
FALSE,
0,
complain_overflow_bitfield,
NULL,
"ADDR32",
TRUE,
0xffffffff,
0xffffffff,
FALSE),
HOWTO (IMAGE_REL_MCORE_PCREL_IMM8BY4,
2,
1,
8,
TRUE,
0,
complain_overflow_bitfield,
mcore_coff_unsupported_reloc,
"IMM8BY4",
FALSE,
0,
0,
TRUE),
HOWTO (IMAGE_REL_MCORE_PCREL_IMM11BY2,
1,
1,
11,
TRUE,
0,
complain_overflow_signed,
NULL,
"IMM11BY2",
FALSE,
0x0,
0x7ff,
TRUE),
HOWTO (IMAGE_REL_MCORE_PCREL_IMM4BY2,
1,
1,
4,
TRUE,
0,
complain_overflow_bitfield,
mcore_coff_unsupported_reloc,
"IMM4BY2",
FALSE,
0,
0,
TRUE),
HOWTO (IMAGE_REL_MCORE_PCREL_32,
0,
2,
32,
TRUE,
0,
complain_overflow_bitfield,
NULL,
"PCREL_32",
FALSE,
0x0,
0xffffffff,
TRUE),
HOWTO (IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2,
1,
1,
11,
TRUE,
0,
complain_overflow_signed,
NULL,
"JSR_IMM11BY2",
FALSE,
0x0,
0x7ff,
TRUE),
HOWTO (IMAGE_REL_MCORE_RVA,
0,
2,
32,
FALSE,
0,
complain_overflow_signed,
NULL,
"MCORE_RVA",
TRUE,
0xffffffff,
0xffffffff,
TRUE)
};
typedef struct coff_mcore_link_hash_table
{
struct coff_link_hash_table root;
bfd * bfd_of_toc_owner;
long int global_toc_size;
long int import_table_size;
long int first_thunk_address;
long int thunk_size;
}
mcore_hash_table;
#define coff_mcore_hash_table(info) \
((mcore_hash_table *) ((info)->hash))
static void
mcore_emit_base_file_entry (info, output_bfd, input_section, reloc_offset)
struct bfd_link_info * info;
bfd * output_bfd;
asection * input_section;
bfd_vma reloc_offset;
{
bfd_vma addr = reloc_offset
- input_section->vma
+ input_section->output_offset
+ input_section->output_section->vma;
if (coff_data (output_bfd)->pe)
addr -= pe_data (output_bfd)->pe_opthdr.ImageBase;
fwrite (&addr, 1, sizeof (addr), (FILE *) info->base_file);
}
static bfd_reloc_status_type
mcore_coff_unsupported_reloc (abfd, reloc_entry, symbol, data, input_section,
output_bfd, error_message)
bfd * abfd;
arelent * reloc_entry;
asymbol * symbol ATTRIBUTE_UNUSED;
PTR data ATTRIBUTE_UNUSED;
asection * input_section ATTRIBUTE_UNUSED;
bfd * output_bfd ATTRIBUTE_UNUSED;
char ** error_message ATTRIBUTE_UNUSED;
{
BFD_ASSERT (reloc_entry->howto != (reloc_howto_type *)0);
_bfd_error_handler (_("%B: Relocation %s (%d) is not currently supported.\n"),
abfd,
reloc_entry->howto->name,
reloc_entry->howto->type);
return bfd_reloc_notsupported;
}
#define HOW2MAP(bfd_rtype, mcore_rtype) \
case bfd_rtype: return & mcore_coff_howto_table [mcore_rtype]
static reloc_howto_type *
mcore_coff_reloc_type_lookup (abfd, code)
bfd * abfd ATTRIBUTE_UNUSED;
bfd_reloc_code_real_type code;
{
switch (code)
{
HOW2MAP (BFD_RELOC_32, IMAGE_REL_MCORE_ADDR32);
HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM8BY4, IMAGE_REL_MCORE_PCREL_IMM8BY4);
HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM11BY2, IMAGE_REL_MCORE_PCREL_IMM11BY2);
HOW2MAP (BFD_RELOC_MCORE_PCREL_IMM4BY2, IMAGE_REL_MCORE_PCREL_IMM4BY2);
HOW2MAP (BFD_RELOC_32_PCREL, IMAGE_REL_MCORE_PCREL_32);
HOW2MAP (BFD_RELOC_MCORE_PCREL_JSR_IMM11BY2, IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2);
HOW2MAP (BFD_RELOC_RVA, IMAGE_REL_MCORE_RVA);
default:
return NULL;
}
}
#undef HOW2MAP
#define RTYPE2HOWTO(cache_ptr, dst) \
(cache_ptr)->howto = mcore_coff_howto_table + (dst)->r_type;
static reloc_howto_type *
coff_mcore_rtype_to_howto (abfd, sec, rel, h, sym, addendp)
bfd * abfd ATTRIBUTE_UNUSED;
asection * sec;
struct internal_reloc * rel;
struct coff_link_hash_entry * h ATTRIBUTE_UNUSED;
struct internal_syment * sym;
bfd_vma * addendp;
{
reloc_howto_type * howto;
if (rel->r_type >= NUM_ELEM (mcore_coff_howto_table))
return NULL;
howto = mcore_coff_howto_table + rel->r_type;
if (rel->r_type == IMAGE_REL_MCORE_RVA)
* addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
else if (howto->pc_relative)
{
* addendp = sec->vma - 2;
if (sym != NULL && sym->n_scnum != 0)
* addendp -= sym->n_value;
}
else
* addendp = 0;
return howto;
}
static bfd_boolean
in_reloc_p (abfd, howto)
bfd * abfd ATTRIBUTE_UNUSED;
reloc_howto_type * howto;
{
return ! howto->pc_relative && howto->type != IMAGE_REL_MCORE_RVA;
}
static bfd_boolean
coff_mcore_relocate_section (output_bfd, info, input_bfd, input_section,
contents, relocs, syms, sections)
bfd * output_bfd;
struct bfd_link_info * info;
bfd * input_bfd;
asection * input_section;
bfd_byte * contents;
struct internal_reloc * relocs;
struct internal_syment * syms;
asection ** sections;
{
struct internal_reloc * rel;
struct internal_reloc * relend;
bfd_boolean hihalf;
bfd_vma hihalf_val;
if (info->relocatable)
return TRUE;
if ( input_bfd->xvec->byteorder != output_bfd->xvec->byteorder
&& output_bfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
{
(*_bfd_error_handler)
(_("%B: compiled for a %s system and target is %s.\n"),
input_bfd,
bfd_big_endian (input_bfd) ? _("big endian") : _("little endian"),
bfd_big_endian (output_bfd) ? _("big endian") : _("little endian"));
bfd_set_error (bfd_error_wrong_format);
return FALSE;
}
hihalf = FALSE;
hihalf_val = 0;
rel = relocs;
relend = rel + input_section->reloc_count;
for (; rel < relend; rel++)
{
long symndx;
struct internal_syment * sym;
bfd_vma val;
bfd_vma addend;
bfd_reloc_status_type rstat;
bfd_byte * loc;
unsigned short r_type = rel->r_type;
reloc_howto_type * howto = NULL;
struct coff_link_hash_entry * h;
const char * my_name;
symndx = rel->r_symndx;
loc = contents + rel->r_vaddr - input_section->vma;
if (symndx == -1)
{
h = NULL;
sym = NULL;
}
else
{
h = obj_coff_sym_hashes (input_bfd)[symndx];
sym = syms + symndx;
}
addend = 0;
howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
sym, & addend);
if (howto == NULL)
return FALSE;
val = 0;
if (h == NULL)
{
if (symndx == -1)
my_name = "*ABS*";
else
{
asection * sec = sections[symndx];
val = (sym->n_value
+ sec->output_section->vma
+ sec->output_offset);
if (sym == NULL)
my_name = "*unknown*";
else if ( sym->_n._n_n._n_zeroes == 0
&& sym->_n._n_n._n_offset != 0)
my_name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
else
{
static char buf [SYMNMLEN + 1];
strncpy (buf, sym->_n._n_name, SYMNMLEN);
buf[SYMNMLEN] = '\0';
my_name = buf;
}
}
}
else
{
if ( h->root.type == bfd_link_hash_defined
|| h->root.type == bfd_link_hash_defweak)
{
asection * sec = h->root.u.def.section;
val = (h->root.u.def.value
+ sec->output_section->vma
+ sec->output_offset);
}
else
{
if (! ((*info->callbacks->undefined_symbol)
(info, h->root.root.string, input_bfd, input_section,
rel->r_vaddr - input_section->vma, TRUE)))
return FALSE;
}
my_name = h->root.root.string;
}
rstat = bfd_reloc_ok;
switch (r_type)
{
default:
_bfd_error_handler (_("%B: unsupported relocation type 0x%02x"),
input_bfd, r_type);
bfd_set_error (bfd_error_bad_value);
return FALSE;
case IMAGE_REL_MCORE_ABSOLUTE:
_bfd_error_handler
(_("Warning: unsupported reloc %s <file %B, section %A>\n"
"sym %ld (%s), r_vaddr %ld (%lx)"),
input_bfd, input_section, howto->name,
rel->r_symndx, my_name, (long) rel->r_vaddr,
(unsigned long) rel->r_vaddr);
break;
case IMAGE_REL_MCORE_PCREL_IMM8BY4:
case IMAGE_REL_MCORE_PCREL_IMM11BY2:
case IMAGE_REL_MCORE_PCREL_IMM4BY2:
case IMAGE_REL_MCORE_PCREL_32:
case IMAGE_REL_MCORE_PCREL_JSR_IMM11BY2:
case IMAGE_REL_MCORE_ADDR32:
rstat = _bfd_relocate_contents (howto, input_bfd, val, loc);
break;
case IMAGE_REL_MCORE_RVA:
rstat = _bfd_final_link_relocate
(howto, input_bfd,
input_section, contents, rel->r_vaddr - input_section->vma,
val, addend);
break;
}
if (info->base_file)
{
if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
mcore_emit_base_file_entry (info, output_bfd, input_section, rel->r_vaddr);
}
switch (rstat)
{
default:
abort ();
case bfd_reloc_ok:
break;
case bfd_reloc_overflow:
if (! ((*info->callbacks->reloc_overflow)
(info, (h ? &h->root : NULL), my_name, howto->name,
(bfd_vma) 0, input_bfd,
input_section, rel->r_vaddr - input_section->vma)))
return FALSE;
}
}
return TRUE;
}
#define coff_bfd_reloc_type_lookup mcore_coff_reloc_type_lookup
#define coff_relocate_section coff_mcore_relocate_section
#define coff_rtype_to_howto coff_mcore_rtype_to_howto
#define SELECT_RELOC(internal, howto) {internal.r_type = howto->type;}
#define SWAP_IN_RELOC_OFFSET H_GET_32
#define SWAP_OUT_RELOC_OFFSET H_PUT_32
#define COFF_PAGE_SIZE 0x1000
#include "coffcode.h"
extern const bfd_target TARGET_LITTLE_SYM;
CREATE_BIG_COFF_TARGET_VEC (TARGET_BIG_SYM, TARGET_BIG_NAME, D_PAGED,
(SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
0, & TARGET_LITTLE_SYM, COFF_SWAP_TABLE)
CREATE_LITTLE_COFF_TARGET_VEC (TARGET_LITTLE_SYM, TARGET_LITTLE_NAME, D_PAGED,
(SEC_CODE | SEC_DATA | SEC_DEBUGGING | SEC_READONLY | SEC_LINK_ONCE | SEC_LINK_DUPLICATES),
0, & TARGET_BIG_SYM, COFF_SWAP_TABLE)