#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
#define ARCH_SIZE 32
#include "nlm/i386-ext.h"
#define Nlm_External_Fixed_Header Nlm32_i386_External_Fixed_Header
#include "libnlm.h"
static reloc_howto_type nlm_i386_abs_howto =
HOWTO (0,
0,
2,
32,
FALSE,
0,
complain_overflow_bitfield,
0,
"32",
TRUE,
0xffffffff,
0xffffffff,
FALSE);
static reloc_howto_type nlm_i386_pcrel_howto =
HOWTO (1,
0,
2,
32,
TRUE,
0,
complain_overflow_signed,
0,
"DISP32",
TRUE,
0xffffffff,
0xffffffff,
TRUE);
static bfd_boolean
nlm_i386_read_reloc (bfd *abfd,
nlmNAME (symbol_type) *sym,
asection **secp,
arelent *rel)
{
bfd_byte temp[4];
bfd_vma val;
const char *name;
if (bfd_bread (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
return FALSE;
val = bfd_get_32 (abfd, temp);
if (sym == NULL)
{
if ((val & NLM_HIBIT) == 0)
name = NLM_INITIALIZED_DATA_NAME;
else
{
name = NLM_CODE_NAME;
val &=~ NLM_HIBIT;
}
rel->sym_ptr_ptr = bfd_get_section_by_name (abfd, name)->symbol_ptr_ptr;
rel->howto = &nlm_i386_abs_howto;
}
else
{
rel->sym_ptr_ptr = NULL;
if ((val & NLM_HIBIT) == 0)
rel->howto = &nlm_i386_pcrel_howto;
else
{
rel->howto = &nlm_i386_abs_howto;
val &=~ NLM_HIBIT;
}
}
if ((val & (NLM_HIBIT >> 1)) == 0)
*secp = bfd_get_section_by_name (abfd, NLM_INITIALIZED_DATA_NAME);
else
{
*secp = bfd_get_section_by_name (abfd, NLM_CODE_NAME);
val &=~ (NLM_HIBIT >> 1);
}
rel->address = val;
rel->addend = 0;
return TRUE;
}
static bfd_boolean
nlm_i386_write_import (bfd * abfd, asection * sec, arelent * rel)
{
asymbol *sym;
bfd_vma val;
bfd_byte temp[4];
if (rel->addend != 0
|| rel->howto == NULL
|| rel->howto->rightshift != 0
|| rel->howto->size != 2
|| rel->howto->bitsize != 32
|| rel->howto->bitpos != 0
|| rel->howto->src_mask != 0xffffffff
|| rel->howto->dst_mask != 0xffffffff)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
}
sym = *rel->sym_ptr_ptr;
val = bfd_get_section_vma (abfd, sec) + rel->address;
if (bfd_get_section_flags (abfd, sec) & SEC_CODE)
{
val -= nlm_get_text_low (abfd);
val |= NLM_HIBIT >> 1;
}
else
val -= nlm_get_data_low (abfd);
if (! bfd_is_und_section (bfd_get_section (sym)))
{
if (rel->howto->pc_relative)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
}
if (bfd_get_section_flags (abfd, bfd_get_section (sym)) & SEC_CODE)
val |= NLM_HIBIT;
}
else
{
if (! rel->howto->pc_relative)
val |= NLM_HIBIT;
else
{
if (! rel->howto->pcrel_offset)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
}
}
}
bfd_put_32 (abfd, val, temp);
if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
return FALSE;
return TRUE;
}
static bfd_boolean
nlm_i386_mangle_relocs (bfd *abfd,
asection *sec,
const PTR data,
bfd_vma offset,
bfd_size_type count)
{
arelent **rel_ptr_ptr, **rel_end;
rel_ptr_ptr = sec->orelocation;
rel_end = rel_ptr_ptr + sec->reloc_count;
for (; rel_ptr_ptr < rel_end; rel_ptr_ptr++)
{
arelent *rel;
asymbol *sym;
bfd_vma addend;
rel = *rel_ptr_ptr;
sym = *rel->sym_ptr_ptr;
if (rel->address < offset
|| rel->address + 4 > offset + count)
continue;
addend = rel->addend + sym->value;
if (((bfd_get_section_flags (abfd, bfd_get_section (sym))
& SEC_LOAD) == 0)
&& ((bfd_get_section_flags (abfd, bfd_get_section (sym))
& SEC_ALLOC) != 0))
addend += nlm_fixed_header (abfd)->dataImageSize;
if (addend != 0
&& rel->howto != NULL
&& rel->howto->rightshift == 0
&& rel->howto->size == 2
&& rel->howto->bitsize == 32
&& rel->howto->bitpos == 0
&& rel->howto->src_mask == 0xffffffff
&& rel->howto->dst_mask == 0xffffffff)
{
bfd_vma val;
val = bfd_get_32 (abfd, (bfd_byte *) data + rel->address - offset);
val += addend;
bfd_put_32 (abfd, val, (bfd_byte *) data + rel->address - offset);
rel->addend = 0;
}
if (rel->howto != NULL
&& rel->howto->pc_relative
&& ! rel->howto->pcrel_offset
&& rel->howto->rightshift == 0
&& rel->howto->size == 2
&& rel->howto->bitsize == 32
&& rel->howto->bitpos == 0
&& rel->howto->src_mask == 0xffffffff
&& rel->howto->dst_mask == 0xffffffff)
{
bfd_vma val;
val = bfd_get_32 (abfd, (bfd_byte *) data + rel->address - offset);
val += rel->address;
bfd_put_32 (abfd, val, (bfd_byte *) data + rel->address - offset);
rel->howto = &nlm_i386_pcrel_howto;
}
}
return TRUE;
}
static bfd_boolean
nlm_i386_read_import (bfd * abfd, nlmNAME (symbol_type) * sym)
{
struct nlm_relent *nlm_relocs;
bfd_size_type rcount;
bfd_byte temp[NLM_TARGET_LONG_SIZE];
unsigned char symlength;
char *name;
if (bfd_bread (& symlength, (bfd_size_type) sizeof (symlength), abfd)
!= sizeof (symlength))
return FALSE;
sym -> symbol.the_bfd = abfd;
name = bfd_alloc (abfd, (bfd_size_type) symlength + 1);
if (name == NULL)
return FALSE;
if (bfd_bread (name, (bfd_size_type) symlength, abfd) != symlength)
return FALSE;
name[symlength] = '\0';
sym -> symbol.name = name;
sym -> symbol.flags = 0;
sym -> symbol.value = 0;
sym -> symbol.section = bfd_und_section_ptr;
if (bfd_bread (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
return FALSE;
rcount = H_GET_32 (abfd, temp);
nlm_relocs = bfd_alloc (abfd, rcount * sizeof (struct nlm_relent));
if (!nlm_relocs)
return FALSE;
sym -> relocs = nlm_relocs;
sym -> rcnt = 0;
while (sym -> rcnt < rcount)
{
asection *section;
if (! nlm_i386_read_reloc (abfd, sym, §ion, &nlm_relocs -> reloc))
return FALSE;
nlm_relocs -> section = section;
nlm_relocs++;
sym -> rcnt++;
}
return TRUE;
}
static bfd_boolean
nlm_i386_write_external (bfd *abfd,
bfd_size_type count,
asymbol *sym,
struct reloc_and_sec *relocs)
{
unsigned int i;
bfd_byte len;
unsigned char temp[NLM_TARGET_LONG_SIZE];
len = strlen (sym->name);
if ((bfd_bwrite (&len, (bfd_size_type) sizeof (bfd_byte), abfd)
!= sizeof (bfd_byte))
|| bfd_bwrite (sym->name, (bfd_size_type) len, abfd) != len)
return FALSE;
bfd_put_32 (abfd, count, temp);
if (bfd_bwrite (temp, (bfd_size_type) sizeof (temp), abfd) != sizeof (temp))
return FALSE;
for (i = 0; i < count; i++)
if (! nlm_i386_write_import (abfd, relocs[i].sec, relocs[i].rel))
return FALSE;
return TRUE;
}
#include "nlmswap.h"
static const struct nlm_backend_data nlm32_i386_backend =
{
"NetWare Loadable Module\032",
sizeof (Nlm32_i386_External_Fixed_Header),
0,
bfd_arch_i386,
0,
FALSE,
0,
0,
nlm_i386_read_reloc,
nlm_i386_mangle_relocs,
nlm_i386_read_import,
nlm_i386_write_import,
0,
0,
nlm_swap_fixed_header_in,
nlm_swap_fixed_header_out,
nlm_i386_write_external,
0,
};
#define TARGET_LITTLE_NAME "nlm32-i386"
#define TARGET_LITTLE_SYM nlmNAME (i386_vec)
#define TARGET_BACKEND_DATA & nlm32_i386_backend
#include "nlm-target.h"