old-cross-binutils/bfd/nlm32-i386.c
David MacKenzie 9783e04a64 Make all callers of malloc or realloc (including via obstacks)
check the result for NULL.  Most set bfd_error to no_memory and
	return in that case; a few are harder to fix, and are marked
	with "FIXME <return type>".

	* elf32-hppa.c (hppa_elf_build_arg_reloc_stub
	hppa_elf_build_long_branch_stub):  Check bfd_make_empty_symbol return.
	* linker.c (_bfd_generic_link_output_symbols
	_bfd_generic_link_write_global_symbol): Ditto
	* section.c (bfd_make_section_anyway): Ditto.

	* tekhex.c (find_chunk tekhex_mkobject): Check bfd_alloc.
	(first_phase): Ditto.  FIXME void
	(tekhex_make_empty_symbol): Check bfd_zalloc.

	* sunos.c (sunos_read_dynamic_info): Check bfd_zalloc.
	(MY(read_dynamic_symbols) MY(read_dynamic_relocs)): Check bfd_alloc.

	* stringhash.c (_bfd_stringtab_hash_newfunc): Check bfd_hash_allocate.

	* srec.c: Indent.
	(fillup_symbols): Check bfd_alloc.  FIXME void
	(srec_mkobject srec_get_section_contents
	srec_set_section_contents): Check bfd_alloc.
	(srec_make_empty_symbol): Check bfd_zalloc.

	* som.c (hppa_som_gen_reloc_type): Check bfd_alloc_by_size_t.
	(make_unique_section): Check bfd_alloc.
	(som_new_section_hook): Check bfd_zalloc.
	(bfd_som_attach_aux_hdr): Ditto.  FIXME void

	* rs6000-core.c (rs6000coff_core_p): Check bfd_zalloc.

	* osf-core.c (osf_core_make_empty_symbol): Check bfd_zalloc.
	(osf_core_core_file_p): Check bfd_alloc.

	* oasys.c (oasys_slurp_symbol_table oasys_archive_p
	oasys_mkobject oasys_object_p oasys_new_section_hook
	oasys_set_section_contents): Check bfd_alloc.
	(oasys_slurp_section_data): Check bfd_zalloc and bfd_alloc.
	(oasys_make_empty_symbol): Check bfd_zalloc.

	* nlmcode.h (nlm_make_empty_symbol): Check bfd_zalloc.
	(nlm_slurp_symbol_table): Check bfd_zalloc and bfd_alloc.

	* nlm32-sparc.c (nlm_sparc_read_import): Check bfd_alloc.

	* nlm32-i386.c (nlm_i386_read_import): Check bfd_alloc.

	* nlm32-alpha.c (nlm_alpha_read_import): Check bfd_alloc.

	* linker.c (_bfd_link_hash_newfunc
	(generic_link_hash_newfunc
	(archive_hash_newfunc
	(_bfd_generic_link_add_one_symbol): Check bfd_hash_allocate.
	(_bfd_generic_final_link
	(_bfd_generic_link_output_symbols
	(default_indirect_link_order): Check bfd_alloc.
	(bfd_new_link_order): Check bfd_alloc_by_size_t.

	* irix-core.c (irix_core_make_empty_symbol): Check bfd_zalloc.

	* ieee.c: Indent.
	(read_id get_symbol get_section_entry ieee_archive_p ieee_object_p
	ieee_slurp_section_data ieee_new_section_hook): Check bfd_alloc.
	(do_one): Check bfd_alloc.  Return a boolean.
	(ieee_slurp_section_data): Check it.
	(init_for_output): Check bfd_alloc.  Return a boolean.
	(ieee_set_section_contents): Check it.
	(do_with_relocs):  Check bfd_alloc.  Return a boolean.
	(ieee_bfd_debug_info_accumulate): Ditto.  FIXME void.
	(ieee_mkobject): Check bfd_zalloc.
	(ieee_make_empty_symbol): Check bfd_zmalloc.

	* hpux-core.c (hpux_core_make_empty_symbol): Check
	bfd_zalloc.

	* hppabsd-core.c (hppabsd_core_make_empty_symbol): Check
	bfd_zalloc.
	(hppabsd_core_core_file_p): Check bfd_zalloc.

	* hp300hpux.c (MY(slurp_symbol_table)): Check bfd_alloc.

	* elfcode.h (elf_new_section_hook): Check bfd_alloc.
	(bfd_section_from_phdr): Ditto.
	(write_relocs): Ditto.  FIXME void
	(elf_map_symbols assign_section_numbers map_program_segments):
	Ditto.  Return a boolean.
	(swap_out_syms): Ditto.  Check elf_map_symbols.
	(elf_slurp_symbol_table): Check bfd_zalloc.
	(elf_slurp_reloca_table): Check bfd_alloc.
	(elf_slurp_reloc_table): Ditto.
	(elf_compute_section_file_positions): Check assign_section_numbers.
	(assign_file_positions_except_relocs): Return a boolean.
	Check map_program_segments.
	(elf_compute_section_file_positions): Check it.

	* elf32-mips.c (mips_elf_final_link): Check bfd_alloc.

	* elf32-hppa.c (hppa_elf_stub_branch_reloc): Check bfd_zmalloc and
	realloc.
	(hppa_elf_stub_reloc): Ditto.
	(hppa_elf_build_arg_reloc_stub): Check bfd_zalloc.
	(hppa_elf_build_long_branch_stub): Ditto.
	(elf32_hppa_backend_symbol_table_processing): Ditto.

	* ecoff.c (ecoff_set_symbol_info): Check bfd_alloc.  Return a boolean.
	(ecoff_slurp_symbol_table): Check it.
	(ecoff_slurp_armap): Check bfd_alloc.
	(ecoff_write_armap): Check bfd_zalloc.
	(ecoff_link_hash_newfunc): Check bfd_hash_allocate and
	_bfd_link_hash_newfunc.
	(ecoff_link_add_externals): Check bfd_alloc.

	* ctor.c (bfd_constructor_entry): Check bfd_alloc.

	* coffgen.c (coff_real_object_p): Check bfd_alloc.
	(coff_renumber_symbols): Check bfd_alloc_by_size_t.  Return a boolean.
	(coff_write_symbol): Check bfd_alloc.  FIXME int
	(coff_write_linenumbers): Check bfd_alloc.  Return a boolean.
	(coff_section_symbol): Check bfd_alloc_by_size_t.
	(coff_get_normalized_symtab): Check bfd_alloc.
	(coff_bfd_make_debug_symbol): Check bfd_zalloc.
	* libcoff-in.h: Change decls of coff_renumber_symbols,
	coff_write_linenumbers.
	* libcoff.h: Rebuilt.
	* coffcode.h (coff_write_object_contents): Check
	coff_renumber_symbols, coff_write_linenumbers.

	* coffcode.h: Indent.
	(coff_add_missing_symbols): Check bfd_alloc_by_size_t.  Return a
	boolean.
	(coff_write_object_contents): Check it.

	* coff-alpha.c (alpha_relocate_section): Check bfd_alloc.
	* coff-mips.c (mips_relocate_section): Ditto.

	* archive.c (bfd_slurp_bsd_armap_f2): Check bfd_alloc value.
	(do_slurp_bsd_armap): Ditto.
	(compute_and_write_armap): Check bfd_realloc value.

	* aoutx.h (translate_from_native_sym_flags): Check bfd_alloc
	return value.  Return boolean value.
	(NAME(aout,make_empty_symbol)): Check bfd_zalloc return value.
	(NAME(aout,slurp_symbol_table)): Check bf_alloc and bfd_zalloc
	return value.
	(add_to_stringtab): Ditto.  FIXME void
	(aout_link_hash_newfunc): Check bfd_hash_allocate return value.
	(aout_link_add_symbols): Check bfd_alloc value.
	(translate_symbol_table): Check translate_from_native_sym_flags.
	* hp300hpux.c (MY(slurp_symbol_table)): Ditto.
	* aoutx.h (aout_link_hash_newfunc): Check _bfd_link_hash_newfunc.

	* opncls.c (bfd_zalloc bfd_realloc): Check result of bfd_alloc.

	* opncls.c (obstack_chunk_alloc): Define as malloc, not
	bfd_xmalloc_by_size_t.
	(_bfd_new_bfd): Check obstack_begin for 0 return.

	* ieee.c (obstack_chunk_alloc): Define as malloc, not
	bfd_xmalloc_by_size_t.
	(ieee_archive_p): Check obstack_begin for 0 return and
	obstack_finish for NULL return.

	* hash.c (obstack_chunk_alloc): Define as malloc, not
	bfd_xmalloc_by_size_t.
	(bfd_hash_table_init_n): Check obstack_begin for 0 return and
	obstack_finish for NULL return.
	(bfd_hash_lookup): Check obstack_alloc for NULL return.

	* ecofflink.c (obstack_chunk_alloc): Define as malloc, not
	bfd_xmalloc_by_size_t.
	bfd_ecoff_debug_accumulate
	bfd_ecoff_debug_accumulate_other): Check obstack_alloc.
	(add_file_shuffle add_memory_shuffle): Check obstack_alloc for
	NULL return.  Return boolean, not void.
	(bfd_ecoff_debug_init): Check obstack_begin for 0 return.
	(bfd_ecoff_debug_accumulate): Check add_file_shuffle
	and add_memory_shuffle return.
	(string_hash_newfunc): Check bfd_hash_allocate and bfd_hash_newfunc.
	(bfd_ecoff_debug_accumulate): Check bfd_alloc.
	(ecoff_add_string): Check add_memory_shuffle return.

	* libbfd-in.h (xmalloc, bfd_xmalloc, bfd_xmalloc_by_size_t):
	Remove decls.
	* libbfd.h: Rebuilt.
1994-02-12 00:45:54 +00:00

476 lines
13 KiB
C

/* Support for 32-bit i386 NLM (NetWare Loadable Module)
Copyright (C) 1993 Free Software Foundation, Inc.
This file is part of BFD, the Binary File Descriptor library.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
#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 boolean nlm_i386_read_reloc
PARAMS ((bfd *, nlmNAME(symbol_type) *, asection **, arelent *));
static boolean nlm_i386_write_import
PARAMS ((bfd *, asection *, arelent *));
static boolean nlm_i386_mangle_relocs
PARAMS ((bfd *, asection *, PTR, bfd_vma, bfd_size_type));
static boolean nlm_i386_read_import
PARAMS ((bfd *, nlmNAME(symbol_type) *));
static boolean nlm_i386_write_external
PARAMS ((bfd *, bfd_size_type, asymbol *, struct reloc_and_sec *));
/* Adjust the reloc location by an absolute value. */
static reloc_howto_type nlm_i386_abs_howto =
HOWTO (0, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
false, /* pc_relative */
0, /* bitpos */
complain_overflow_bitfield, /* complain_on_overflow */
0, /* special_function */
"32", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
false); /* pcrel_offset */
/* Adjust the reloc location by a PC relative displacement. */
static reloc_howto_type nlm_i386_pcrel_howto =
HOWTO (1, /* type */
0, /* rightshift */
2, /* size (0 = byte, 1 = short, 2 = long) */
32, /* bitsize */
true, /* pc_relative */
0, /* bitpos */
complain_overflow_signed, /* complain_on_overflow */
0, /* special_function */
"DISP32", /* name */
true, /* partial_inplace */
0xffffffff, /* src_mask */
0xffffffff, /* dst_mask */
true); /* pcrel_offset */
/* Read a NetWare i386 reloc. */
static boolean
nlm_i386_read_reloc (abfd, sym, secp, rel)
bfd *abfd;
nlmNAME(symbol_type) *sym;
asection **secp;
arelent *rel;
{
bfd_byte temp[4];
bfd_vma val;
const char *name;
if (bfd_read (temp, sizeof (temp), 1, abfd) != sizeof (temp))
{
bfd_error = system_call_error;
return false;
}
val = bfd_get_32 (abfd, temp);
/* The value is an offset into either the code or data segment.
This is the location which needs to be adjusted.
If this is a relocation fixup rather than an imported symbol (the
sym argument is NULL) then the high bit is 0 if the location
needs to be adjusted by the address of the data segment, or 1 if
the location needs to be adjusted by the address of the code
segment. If this is an imported symbol, then the high bit is 0
if the location is 0 if the location should be adjusted by the
offset to the symbol, or 1 if the location should adjusted by the
absolute value of the symbol.
The second most significant bit is 0 if the value is an offset
into the data segment, or 1 if the value is an offset into the
code segment.
All this translates fairly easily into a BFD reloc. */
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
{
/* In this case we do not need to set the sym_ptr_ptr field. */
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;
}
/* Write a NetWare i386 reloc. */
static boolean
nlm_i386_write_import (abfd, sec, rel)
bfd *abfd;
asection *sec;
arelent *rel;
{
asymbol *sym;
bfd_vma val;
bfd_byte temp[4];
/* NetWare only supports two kinds of relocs. We should check
special_function here, as well, but at the moment coff-i386
relocs uses a special_function which does not affect what we do
here. */
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_error = invalid_operation;
return false;
}
sym = *rel->sym_ptr_ptr;
/* The value we write out is the offset into the appropriate
segment. This offset is the section vma, adjusted by the vma of
the lowest section in that segment, plus the address of the
relocation. */
val = bfd_get_section_vma (abfd, sec) + rel->address;
/* The second most significant bit is 0 if the value is an offset
into the data segment, or 1 if the value is an offset into the
code segment. */
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_get_section (sym) != &bfd_und_section)
{
/* NetWare only supports absolute internal relocs. */
if (rel->howto->pc_relative)
{
bfd_error = invalid_operation;
return false;
}
/* The high bit is 1 if the reloc is against the code section, 0
if against the data section. */
if (bfd_get_section_flags (abfd, bfd_get_section (sym)) & SEC_CODE)
val |= NLM_HIBIT;
}
else
{
/* The high bit is 1 if this is an absolute reloc, 0 if it is PC
relative. */
if (! rel->howto->pc_relative)
val |= NLM_HIBIT;
else
{
/* PC relative relocs on NetWare must be pcrel_offset. */
if (! rel->howto->pcrel_offset)
{
bfd_error = invalid_operation;
return false;
}
}
}
bfd_put_32 (abfd, val, temp);
if (bfd_write (temp, sizeof (temp), 1, abfd) != sizeof (temp))
{
bfd_error = system_call_error;
return false;
}
return true;
}
/* I want to be able to use objcopy to turn a i386 a.out or COFF file
into a NetWare i386 module. That means that the relocs from the
source file have to be mapped into relocs that apply to the target
file. This function is called by nlm_set_section_contents to give
it a chance to rework the relocs.
This is actually a fairly general concept. However, this is not a
general implementation. */
static boolean
nlm_i386_mangle_relocs (abfd, sec, data, offset, count)
bfd *abfd;
asection *sec;
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;
/* Note that no serious harm will ensue if we fail to change a
reloc. We will wind up failing in nlm_i386_write_import. */
/* Make sure this reloc is within the data we have. We only 4
byte relocs here, so we insist on having 4 bytes. */
if (rel->address < offset
|| rel->address + 4 > offset + count)
continue;
/* NetWare doesn't support reloc addends, so we get rid of them
here by simply adding them into the object data. We handle
the symbol value, if any, the same way. */
addend = rel->addend + sym->value;
/* The value of a symbol is the offset into the section. If the
symbol is in the .bss segment, we need to include the size of
the data segment in the offset as well. Fortunately, we know
that at this point the size of the data section is in the NLM
header. */
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;
}
/* NetWare uses a reloc with pcrel_offset set. We adjust
pc_relative relocs accordingly. We are going to change the
howto field, so we can only do this if the current one is
compatible. We should check special_function here, but at
the moment coff-i386 uses a special_function which does not
affect what we are doing here. */
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;
/* When pcrel_offset is not set, it means that the negative
of the address of the memory location is stored in the
memory location. We must add it back in. */
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;
}
/* Read a NetWare i386 import record */
static boolean
nlm_i386_read_import (abfd, sym)
bfd *abfd;
nlmNAME(symbol_type) *sym;
{
struct nlm_relent *nlm_relocs; /* relocation records for symbol */
bfd_size_type rcount; /* number of relocs */
bfd_byte temp[NLM_TARGET_LONG_SIZE]; /* temporary 32-bit value */
unsigned char symlength; /* length of symbol name */
if (bfd_read ((PTR) &symlength, sizeof (symlength), 1, abfd)
!= sizeof (symlength))
{
bfd_error = system_call_error;
return (false);
}
sym -> symbol.the_bfd = abfd;
sym -> symbol.name = bfd_alloc (abfd, symlength + 1);
if (!sym -> symbol.name)
{
bfd_error = no_memory;
return false;
}
if (bfd_read ((PTR) sym -> symbol.name, symlength, 1, abfd)
!= symlength)
{
bfd_error = system_call_error;
return (false);
}
sym -> symbol.flags = 0;
sym -> symbol.value = 0;
sym -> symbol.section = &bfd_und_section;
if (bfd_read ((PTR) temp, sizeof (temp), 1, abfd) != sizeof (temp))
{
bfd_error = system_call_error;
return (false);
}
rcount = bfd_h_get_32 (abfd, temp);
nlm_relocs = ((struct nlm_relent *)
bfd_alloc (abfd, rcount * sizeof (struct nlm_relent)));
if (!nlm_relocs)
{
bfd_error = no_memory;
return false;
}
sym -> relocs = nlm_relocs;
sym -> rcnt = 0;
while (sym -> rcnt < rcount)
{
asection *section;
if (nlm_i386_read_reloc (abfd, sym, &section,
&nlm_relocs -> reloc)
== false)
return false;
nlm_relocs -> section = section;
nlm_relocs++;
sym -> rcnt++;
}
return true;
}
/* Write out an external reference. */
static boolean
nlm_i386_write_external (abfd, count, sym, relocs)
bfd *abfd;
bfd_size_type count;
asymbol *sym;
struct reloc_and_sec *relocs;
{
int i;
bfd_byte len;
unsigned char temp[NLM_TARGET_LONG_SIZE];
len = strlen (sym->name);
if ((bfd_write (&len, sizeof (bfd_byte), 1, abfd) != sizeof(bfd_byte))
|| bfd_write (sym->name, len, 1, abfd) != len)
{
bfd_error = system_call_error;
return false;
}
bfd_put_32 (abfd, count, temp);
if (bfd_write (temp, sizeof(temp), 1, abfd) != sizeof (temp))
{
bfd_error = system_call_error;
return false;
}
for (i = 0; i < count; i++)
{
if (nlm_i386_write_import (abfd, relocs[i].sec,
relocs[i].rel) == false)
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, /* optional_prefix_size */
bfd_arch_i386,
0,
false,
0, /* backend_object_p */
0, /* write_prefix_func */
nlm_i386_read_reloc,
nlm_i386_mangle_relocs,
nlm_i386_read_import,
nlm_i386_write_import,
0, /* set_public_section */
0, /* get_public_offset */
nlm_swap_fixed_header_in,
nlm_swap_fixed_header_out,
nlm_i386_write_external,
0, /* write_export */
};
#define TARGET_LITTLE_NAME "nlm32-i386"
#define TARGET_LITTLE_SYM nlmNAME(i386_vec)
#define TARGET_BACKEND_DATA &nlm32_i386_backend
#include "nlm-target.h"