#include "bfd.h"
#include "sysdep.h"
#include "libbfd.h"
#include "libiberty.h"
#ifndef HAVE_GETPAGESIZE
#define getpagesize() 2048
#endif
bfd_boolean
bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
}
bfd_boolean
bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
{
return TRUE;
}
void *
bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_invalid_operation);
return NULL;
}
int
bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
{
return 0;
}
unsigned int
bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
{
return 0;
}
long
bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
{
return 0;
}
long
_bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_invalid_operation);
return -1;
}
void
bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
{
}
bfd_boolean
_bfd_nocore_core_file_matches_executable_p
(bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
}
char *
_bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_invalid_operation);
return NULL;
}
int
_bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_invalid_operation);
return 0;
}
const bfd_target *
_bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
{
bfd_set_error (bfd_error_wrong_format);
return 0;
}
void *
bfd_malloc (bfd_size_type size)
{
void *ptr;
if (size != (size_t) size)
{
bfd_set_error (bfd_error_no_memory);
return NULL;
}
ptr = malloc ((size_t) size);
if (ptr == NULL && (size_t) size != 0)
bfd_set_error (bfd_error_no_memory);
return ptr;
}
void *
bfd_realloc (void *ptr, bfd_size_type size)
{
void *ret;
if (size != (size_t) size)
{
bfd_set_error (bfd_error_no_memory);
return NULL;
}
if (ptr == NULL)
ret = malloc ((size_t) size);
else
ret = realloc (ptr, (size_t) size);
if (ret == NULL && (size_t) size != 0)
bfd_set_error (bfd_error_no_memory);
return ret;
}
void *
bfd_zmalloc (bfd_size_type size)
{
void *ptr;
if (size != (size_t) size)
{
bfd_set_error (bfd_error_no_memory);
return NULL;
}
ptr = malloc ((size_t) size);
if ((size_t) size != 0)
{
if (ptr == NULL)
bfd_set_error (bfd_error_no_memory);
else
memset (ptr, 0, (size_t) size);
}
return ptr;
}
bfd_boolean
bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
{
bfd_byte buffer[4];
bfd_putb32 ((bfd_vma) i, buffer);
return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
}
#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
#define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
#define EIGHT_GAZILLION ((BFD_HOST_64_BIT) 1 << 63)
#define COERCE64(x) \
(((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
bfd_vma
bfd_getb16 (const bfd_byte *addr)
{
return (addr[0] << 8) | addr[1];
}
bfd_vma
bfd_getl16 (const bfd_byte *addr)
{
return (addr[1] << 8) | addr[0];
}
bfd_signed_vma
bfd_getb_signed_16 (const bfd_byte *addr)
{
return COERCE16 ((addr[0] << 8) | addr[1]);
}
bfd_signed_vma
bfd_getl_signed_16 (const bfd_byte *addr)
{
return COERCE16 ((addr[1] << 8) | addr[0]);
}
void
bfd_putb16 (bfd_vma data, bfd_byte *addr)
{
addr[0] = (bfd_byte) (data >> 8);
addr[1] = (bfd_byte) data;
}
void
bfd_putl16 (bfd_vma data, bfd_byte *addr)
{
addr[0] = (bfd_byte) data;
addr[1] = (bfd_byte) (data >> 8);
}
bfd_vma
bfd_getb32 (const bfd_byte *addr)
{
unsigned long v;
v = (unsigned long) addr[0] << 24;
v |= (unsigned long) addr[1] << 16;
v |= (unsigned long) addr[2] << 8;
v |= (unsigned long) addr[3];
return v;
}
bfd_vma
bfd_getl32 (const bfd_byte *addr)
{
unsigned long v;
v = (unsigned long) addr[0];
v |= (unsigned long) addr[1] << 8;
v |= (unsigned long) addr[2] << 16;
v |= (unsigned long) addr[3] << 24;
return v;
}
bfd_signed_vma
bfd_getb_signed_32 (const bfd_byte *addr)
{
unsigned long v;
v = (unsigned long) addr[0] << 24;
v |= (unsigned long) addr[1] << 16;
v |= (unsigned long) addr[2] << 8;
v |= (unsigned long) addr[3];
return COERCE32 (v);
}
bfd_signed_vma
bfd_getl_signed_32 (const bfd_byte *addr)
{
unsigned long v;
v = (unsigned long) addr[0];
v |= (unsigned long) addr[1] << 8;
v |= (unsigned long) addr[2] << 16;
v |= (unsigned long) addr[3] << 24;
return COERCE32 (v);
}
bfd_vma
bfd_getb64 (const bfd_byte *addr ATTRIBUTE_UNUSED)
{
#ifdef BFD64
bfd_vma v;
v = addr[0]; v <<= 8;
v |= addr[1]; v <<= 8;
v |= addr[2]; v <<= 8;
v |= addr[3]; v <<= 8;
v |= addr[4]; v <<= 8;
v |= addr[5]; v <<= 8;
v |= addr[6]; v <<= 8;
v |= addr[7];
return v;
#else
BFD_FAIL();
return 0;
#endif
}
bfd_vma
bfd_getl64 (const bfd_byte *addr ATTRIBUTE_UNUSED)
{
#ifdef BFD64
bfd_vma v;
v = addr[7]; v <<= 8;
v |= addr[6]; v <<= 8;
v |= addr[5]; v <<= 8;
v |= addr[4]; v <<= 8;
v |= addr[3]; v <<= 8;
v |= addr[2]; v <<= 8;
v |= addr[1]; v <<= 8;
v |= addr[0];
return v;
#else
BFD_FAIL();
return 0;
#endif
}
bfd_signed_vma
bfd_getb_signed_64 (addr)
register const bfd_byte *addr ATTRIBUTE_UNUSED;
{
#ifdef BFD64
bfd_vma v;
v = addr[0]; v <<= 8;
v |= addr[1]; v <<= 8;
v |= addr[2]; v <<= 8;
v |= addr[3]; v <<= 8;
v |= addr[4]; v <<= 8;
v |= addr[5]; v <<= 8;
v |= addr[6]; v <<= 8;
v |= addr[7];
return COERCE64 (v);
#else
BFD_FAIL();
return 0;
#endif
}
bfd_signed_vma
bfd_getl_signed_64 (addr)
register const bfd_byte *addr ATTRIBUTE_UNUSED;
{
#ifdef BFD64
bfd_vma v;
v = addr[7]; v <<= 8;
v |= addr[6]; v <<= 8;
v |= addr[5]; v <<= 8;
v |= addr[4]; v <<= 8;
v |= addr[3]; v <<= 8;
v |= addr[2]; v <<= 8;
v |= addr[1]; v <<= 8;
v |= addr[0];
return COERCE64 (v);
#else
BFD_FAIL();
return 0;
#endif
}
void
bfd_putb32 (bfd_vma data, bfd_byte *addr)
{
addr[0] = (bfd_byte) (data >> 24);
addr[1] = (bfd_byte) (data >> 16);
addr[2] = (bfd_byte) (data >> 8);
addr[3] = (bfd_byte) data;
}
void
bfd_putl32 (bfd_vma data, bfd_byte *addr)
{
addr[0] = (bfd_byte) data;
addr[1] = (bfd_byte) (data >> 8);
addr[2] = (bfd_byte) (data >> 16);
addr[3] = (bfd_byte) (data >> 24);
}
void
bfd_putb64 (bfd_vma data ATTRIBUTE_UNUSED, bfd_byte *addr ATTRIBUTE_UNUSED)
{
#ifdef BFD64
addr[0] = (bfd_byte) (data >> (7*8));
addr[1] = (bfd_byte) (data >> (6*8));
addr[2] = (bfd_byte) (data >> (5*8));
addr[3] = (bfd_byte) (data >> (4*8));
addr[4] = (bfd_byte) (data >> (3*8));
addr[5] = (bfd_byte) (data >> (2*8));
addr[6] = (bfd_byte) (data >> (1*8));
addr[7] = (bfd_byte) (data >> (0*8));
#else
BFD_FAIL();
#endif
}
void
bfd_putl64 (bfd_vma data ATTRIBUTE_UNUSED, bfd_byte *addr ATTRIBUTE_UNUSED)
{
#ifdef BFD64
addr[7] = (bfd_byte) (data >> (7*8));
addr[6] = (bfd_byte) (data >> (6*8));
addr[5] = (bfd_byte) (data >> (5*8));
addr[4] = (bfd_byte) (data >> (4*8));
addr[3] = (bfd_byte) (data >> (3*8));
addr[2] = (bfd_byte) (data >> (2*8));
addr[1] = (bfd_byte) (data >> (1*8));
addr[0] = (bfd_byte) (data >> (0*8));
#else
BFD_FAIL();
#endif
}
void
bfd_put_bits (bfd_vma data, bfd_byte *addr, int bits, bfd_boolean big_p)
{
int i;
int bytes;
if (bits % 8 != 0)
abort ();
bytes = bits / 8;
for (i = 0; i < bytes; i++)
{
int index = big_p ? bytes - i - 1 : i;
addr[index] = (bfd_byte) data;
data >>= 8;
}
}
bfd_vma
bfd_get_bits (bfd_byte *addr, int bits, bfd_boolean big_p)
{
bfd_vma data;
int i;
int bytes;
if (bits % 8 != 0)
abort ();
data = 0;
bytes = bits / 8;
for (i = 0; i < bytes; i++)
{
int index = big_p ? i : bytes - i - 1;
data = (data << 8) | addr[index];
}
return data;
}
bfd_boolean
_bfd_generic_get_section_contents (bfd *abfd,
sec_ptr section,
void *location,
file_ptr offset,
bfd_size_type count)
{
if (count == 0)
return TRUE;
if (offset + count > section->_raw_size)
{
bfd_set_error (bfd_error_invalid_operation);
return FALSE;
}
if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
|| bfd_bread (location, count, abfd) != count)
return FALSE;
return TRUE;
}
bfd_boolean
_bfd_generic_get_section_contents_in_window
(bfd *abfd ATTRIBUTE_UNUSED,
sec_ptr section ATTRIBUTE_UNUSED,
bfd_window *w ATTRIBUTE_UNUSED,
file_ptr offset ATTRIBUTE_UNUSED,
bfd_size_type count ATTRIBUTE_UNUSED)
{
return _bfd_generic_get_section_contents_in_window_with_mode
(abfd, section, w, offset, count, FALSE);
}
bfd_boolean
_bfd_generic_get_section_contents_in_window_with_mode
(bfd *abfd ATTRIBUTE_UNUSED,
sec_ptr section ATTRIBUTE_UNUSED,
bfd_window *w ATTRIBUTE_UNUSED,
file_ptr offset ATTRIBUTE_UNUSED,
bfd_size_type count ATTRIBUTE_UNUSED,
bfd_boolean mode ATTRIBUTE_UNUSED)
{
if (count == 0)
return TRUE;
if (abfd->xvec->_bfd_get_section_contents
!= _bfd_generic_get_section_contents)
{
bfd_free_window (w);
w->i = bfd_zmalloc (sizeof (bfd_window_internal));
if (w->i == NULL)
return FALSE;
w->i->data = bfd_malloc (count);
if (w->i->data == NULL)
{
free (w->i);
w->i = NULL;
return FALSE;
}
w->i->mapped = 0;
w->i->refcount = 1;
w->size = w->i->size = count;
w->data = w->i->data;
return bfd_get_section_contents (abfd, section, w->data, offset, count);
}
if ((bfd_size_type) (offset+count) > section->_raw_size
|| (! bfd_get_file_window (abfd, section->filepos + offset, count, w, mode)))
return FALSE;
return TRUE;
}
bfd_boolean
_bfd_generic_set_section_contents (bfd *abfd,
sec_ptr section,
const void *location,
file_ptr offset,
bfd_size_type count)
{
if (count == 0)
return TRUE;
if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
|| bfd_bwrite (location, count, abfd) != count)
return FALSE;
return TRUE;
}
unsigned int
bfd_log2 (bfd_vma x)
{
unsigned int result = 0;
while ((x = (x >> 1)) != 0)
++result;
return result;
}
bfd_boolean
bfd_generic_is_local_label_name (bfd *abfd, const char *name)
{
char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
return name[0] == locals_prefix;
}
bfd_boolean
_bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
{
if (ibfd->xvec->byteorder != obfd->xvec->byteorder
&& ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
&& obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
{
const char *msg;
if (bfd_big_endian (ibfd))
msg = _("%s: compiled for a big endian system and target is little endian");
else
msg = _("%s: compiled for a little endian system and target is big endian");
(*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
bfd_set_error (bfd_error_wrong_format);
return FALSE;
}
return TRUE;
}
void
warn_deprecated (const char *what,
const char *file,
int line,
const char *func)
{
static size_t mask = 0;
if (~(size_t) func & ~mask)
{
if (func)
fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
what, file, line, func);
else
fprintf (stderr, _("Deprecated %s called\n"), what);
mask |= ~(size_t) func;
}
}