Example #1
0
value camlidl_libbfd_bfd_get_section_contents(
	value _v_abfd,
	value _v_section,
	value _v_offset,
	value _v_count)
{
  bfdp abfd; /*in*/
  section_ptr section; /*in*/
  char *location; /*out*/
  file_ptr offset; /*in*/
  bfd_size_type count; /*in*/
  bfd_boolean _res;
  struct camlidl_ctx_struct _ctxs = { CAMLIDL_TRANSIENT, NULL };
  camlidl_ctx _ctx = &_ctxs;
  value _vresult;
  value _vres[2] = { 0, 0, };

  camlidl_ml2c_libbfd_bfdp(_v_abfd, &abfd, _ctx);
  camlidl_ml2c_libbfd_section_ptr(_v_section, &section, _ctx);
  camlidl_ml2c_libbfd_file_ptr(_v_offset, &offset, _ctx);
  camlidl_ml2c_libbfd_bfd_size_type(_v_count, &count, _ctx);
  location = stat_alloc(count * sizeof(char ));
  _res = bfd_get_section_contents(abfd, section, location, offset, count);
  Begin_roots_block(_vres, 2)
    _vres[0] = camlidl_c2ml_libbfd_bfd_boolean(&_res, _ctx);
    _vres[1] = alloc_bigarray_dims(
            BIGARRAY_UINT8 | BIGARRAY_C_LAYOUT | BIGARRAY_MANAGED,
            1, location, count);
    _vresult = camlidl_alloc_small(2, 0);
    Field(_vresult, 0) = _vres[0];
    Field(_vresult, 1) = _vres[1];
  End_roots()
  camlidl_free(_ctx);
  return _vresult;
}
Example #2
0
bfd_boolean
bfd_init_section_compress_status (bfd *abfd, sec_ptr sec)
{
  bfd_size_type uncompressed_size;
  bfd_byte *uncompressed_buffer;
  bfd_boolean ret;

  /* Error if not opened for read.  */
  if (abfd->direction != read_direction
      || sec->size == 0
      || sec->rawsize != 0
      || sec->contents != NULL
      || sec->compress_status != COMPRESS_SECTION_NONE)
    {
      bfd_set_error (bfd_error_invalid_operation);
      return FALSE;
    }

  /* Read in the full section contents and compress it.  */
  uncompressed_size = sec->size;
  uncompressed_buffer = (bfd_byte *) bfd_malloc (uncompressed_size);
  if (!bfd_get_section_contents (abfd, sec, uncompressed_buffer,
				 0, uncompressed_size))
    ret = FALSE;
  else
    {
      uncompressed_size = bfd_compress_section_contents (abfd, sec,
							 uncompressed_buffer,
							 uncompressed_size);
      ret = uncompressed_size != 0;
    }

  return ret;
}
uint64_t
i386fbsd_core_read_xcr0 (bfd *abfd)
{
  asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
  uint64_t xcr0;

  if (xstate)
    {
      size_t size = bfd_section_size (abfd, xstate);

      /* Check extended state size.  */
      if (size < X86_XSTATE_AVX_SIZE)
	xcr0 = X86_XSTATE_SSE_MASK;
      else
	{
	  char contents[8];

	  if (! bfd_get_section_contents (abfd, xstate, contents,
					  I386_FBSD_XSAVE_XCR0_OFFSET,
					  8))
	    {
	      warning (_("Couldn't read `xcr0' bytes from "
			 "`.reg-xstate' section in core file."));
	      return X86_XSTATE_SSE_MASK;
	    }

	  xcr0 = bfd_get_64 (abfd, contents);
	}
    }
  else
    xcr0 = X86_XSTATE_SSE_MASK;

  return xcr0;
}
Example #4
0
static LdInfo *
rs6000_core_ldinfo (bfd *abfd)
{
  struct bfd_section *ldinfo_sec;
  int ldinfo_size;
  gdb_byte *ldinfo_buf;
  struct cleanup *cleanup;

  ldinfo_sec = bfd_get_section_by_name (abfd, ".ldinfo");
  if (ldinfo_sec == NULL)
    error (_("cannot find .ldinfo section from core file: %s\n"),
	   bfd_errmsg (bfd_get_error ()));
  ldinfo_size = bfd_get_section_size (ldinfo_sec);

  ldinfo_buf = xmalloc (ldinfo_size);
  cleanup = make_cleanup (xfree, ldinfo_buf);

  if (! bfd_get_section_contents (abfd, ldinfo_sec,
				  ldinfo_buf, 0, ldinfo_size))
    error (_("unable to read .ldinfo section from core file: %s\n"),
	   bfd_errmsg (bfd_get_error ()));

  discard_cleanups (cleanup);
  return (LdInfo *) ldinfo_buf;
}
uint64_t
i386_linux_core_read_xcr0 (struct gdbarch *gdbarch,
			   struct target_ops *target, bfd *abfd)
{
  asection *xstate = bfd_get_section_by_name (abfd, ".reg-xstate");
  uint64_t xcr0;

  if (xstate)
    {
      size_t size = bfd_section_size (abfd, xstate);

      /* Check extended state size.  */
      if (size < I386_XSTATE_AVX_SIZE)
	xcr0 = I386_XSTATE_SSE_MASK;
      else
	{
	  char contents[8];

	  if (! bfd_get_section_contents (abfd, xstate, contents,
					  I386_LINUX_XSAVE_XCR0_OFFSET,
					  8))
	    {
	      warning (_("Couldn't read `xcr0' bytes from "
			 "`.reg-xstate' section in core file."));
	      return 0;
	    }

	  xcr0 = bfd_get_64 (abfd, contents);
	}
    }
  else
    xcr0 = 0;

  return xcr0;
}
Example #6
0
value get_section_data_internal( bhp _p )
{
    CAMLparam0();
    CAMLlocal4( data, v, str, tupl );

    bh* p = (bh*) _p;
    struct bfd* abfd = p->bfdp;
    asection *sect;
    bfd_size_type datasize = 0;

    data = Val_emptylist;

    if ( p->is_from_file ) {

        for ( sect = abfd->sections; sect != NULL; sect = sect->next ) {
            datasize = bfd_get_section_size( sect );
            str = caml_alloc_string( datasize );
            bfd_get_section_contents( abfd, sect,
                                      (bfd_byte*)String_val(str),
                                      0, datasize );
            tupl = caml_alloc_tuple( 3 );
            Store_field( tupl, 0, str );
            Store_field( tupl, 1, caml_copy_int64( sect->vma ) );
            Store_field( tupl, 2, caml_copy_int64( sect->vma + datasize ) );
            v = caml_alloc_small( 2, 0 );
            Field( v, 0 ) = tupl;
            Field( v, 1 ) = data;
            data = v;
        }

    }

    CAMLreturn( data );
}
Example #7
0
File: rddbg.c Project: CromFr/gdb
static bfd_boolean
read_ieee_debugging_info (bfd *abfd, void *dhandle, bfd_boolean *pfound)
{
  asection *dsec;
  bfd_size_type size;
  bfd_byte *contents;

  /* The BFD backend puts the debugging information into a section
     named .debug.  */

  dsec = bfd_get_section_by_name (abfd, ".debug");
  if (dsec == NULL)
    return TRUE;

  size = bfd_section_size (abfd, dsec);
  contents = (bfd_byte *) xmalloc (size);
  if (! bfd_get_section_contents (abfd, dsec, contents, 0, size))
    return FALSE;

  if (! parse_ieee (dhandle, abfd, contents, size))
    return FALSE;

  free (contents);

  *pfound = TRUE;

  return TRUE;
}
Example #8
0
static void
bfd_pef_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, (void *) file, symbol);
      fprintf (file, " %-5s %s", symbol->section->name, symbol->name);
      if (CONST_STRNEQ (symbol->name, "__traceback_"))
	{
	  unsigned char *buf = xmalloc (symbol->udata.i);
	  size_t offset = symbol->value + 4;
	  size_t len = symbol->udata.i;
	  int ret;

	  bfd_get_section_contents (abfd, symbol->section, buf, offset, len);
	  ret = bfd_pef_parse_traceback_table (abfd, symbol->section, buf,
					       len, 0, NULL, file);
	  if (ret < 0)
	    fprintf (file, " [ERROR]");
	  free (buf);
	}
    }
}
Example #9
0
uv_err_t UVDBFDPatSections::get(struct bfd_section *bfdSectionIn, uint32_t sectionSize, UVDBFDPatSection **uvdSectionOut)
{
	uv_err_t findRc = UV_ERR_GENERAL;
	
	uv_assert_ret(uvdSectionOut);
	findRc = find(bfdSectionIn, uvdSectionOut);
	if( findRc == UV_ERR_NOTFOUND )
	{
		UVDBFDPatSection *uvdSection = NULL;

		uvdSection = new UVDBFDPatSection();
		uv_assert_ret(uvdSection);
		uvdSection->m_section = bfdSectionIn;
		uvdSection->m_content = (unsigned char *)malloc(sectionSize);
		bfd_get_section_contents(m_core->m_bfd, bfdSectionIn, uvdSection->m_content, 0, sectionSize);
		
		uv_assert_err_ret(add(uvdSection));
		*uvdSectionOut = uvdSection;
	}
	else
	{
		uv_assert_err_ret(findRc);
	}

	return UV_ERR_OK;
}
Example #10
0
static void
sparclite_serial_write (bfd *from_bfd, asection *from_sec, file_ptr from_addr,
			bfd_vma to_addr, int len)
{
  char buffer[4 + 4 + WRITESIZE];	/* addr + len + data */
  unsigned char checksum;
  int i;

  store_unsigned_integer (buffer, 4, to_addr);	/* Address */
  store_unsigned_integer (buffer + 4, 4, len);	/* Length */

  bfd_get_section_contents (from_bfd, from_sec, buffer + 8, from_addr, len);

  checksum = 0;
  for (i = 0; i < len; i++)
    checksum += buffer[8 + i];

  i = send_resp (remote_desc, 0x01);

  if (i != 0x5a)
    error ("Bad response from load command (0x%x)", i);

  debug_serial_write (remote_desc, buffer, 4 + 4 + len);
  i = readchar (remote_desc, remote_timeout);

  if (i != checksum)
    error ("Bad checksum from load command (0x%x)", i);
}
Example #11
0
void
core_target::get_core_register_section (struct regcache *regcache,
					const struct regset *regset,
					const char *name,
					int section_min_size,
					int which,
					const char *human_name,
					bool required)
{
  struct bfd_section *section;
  bfd_size_type size;
  char *contents;
  bool variable_size_section = (regset != NULL
				&& regset->flags & REGSET_VARIABLE_SIZE);

  thread_section_name section_name (name, regcache->ptid ());

  section = bfd_get_section_by_name (core_bfd, section_name.c_str ());
  if (! section)
    {
      if (required)
	warning (_("Couldn't find %s registers in core file."),
		 human_name);
      return;
    }

  size = bfd_section_size (core_bfd, section);
  if (size < section_min_size)
    {
      warning (_("Section `%s' in core file too small."),
	       section_name.c_str ());
      return;
    }
  if (size != section_min_size && !variable_size_section)
    {
      warning (_("Unexpected size of section `%s' in core file."),
	       section_name.c_str ());
    }

  contents = (char *) alloca (size);
  if (! bfd_get_section_contents (core_bfd, section, contents,
				  (file_ptr) 0, size))
    {
      warning (_("Couldn't read %s registers from `%s' section in core file."),
	       human_name, section_name.c_str ());
      return;
    }

  if (regset != NULL)
    {
      regset->supply_regset (regset, regcache, -1, contents, size);
      return;
    }

  gdb_assert (m_core_vec != nullptr);
  m_core_vec->core_read_registers (regcache, contents, size, which,
				   ((CORE_ADDR)
				    bfd_section_vma (core_bfd, section)));
}
Example #12
0
void
coff_solib_add (char *arg_string, int from_tty, struct target_ops *target, int readsyms)
{
  asection *libsect;

  if (!readsyms)
    return;

  libsect = bfd_get_section_by_name (exec_bfd, ".lib");

  if (libsect)
    {
      int libsize;
      unsigned char *lib;
      struct libent
	{
	  bfd_byte len[4];
	  bfd_byte nameoffset[4];
	};

      libsize = bfd_section_size (exec_bfd, libsect);

      lib = (unsigned char *) alloca (libsize);

      bfd_get_section_contents (exec_bfd, libsect, lib, 0, libsize);

      while (libsize > 0)
	{
	  struct libent *ent;
	  struct objfile *objfile;
	  int len, nameoffset;
	  char *filename;

	  ent = (struct libent *) lib;

	  len = bfd_get_32 (exec_bfd, ent->len);

	  nameoffset = bfd_get_32 (exec_bfd, ent->nameoffset);

	  if (len <= 0)
	    break;

	  filename = (char *) ent + nameoffset * 4;

	  objfile = symbol_file_add (filename, from_tty,
				     NULL,	/* no offsets */
				     0,		/* not mainline */
				     OBJF_SHARED);	/* flags */

	  libsize -= len * 4;
	  lib += len * 4;
	}

      /* Getting new symbols may change our opinion about what is
         frameless.  */
      reinit_frame_cache ();
    }
}
Example #13
0
bfd_boolean
bfd_is_section_compressed_with_header (bfd *abfd, sec_ptr sec,
                                       int *compression_header_size_p,
                                       bfd_size_type *uncompressed_size_p)
{
    bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
    int compression_header_size;
    int header_size;
    unsigned int saved = sec->compress_status;
    bfd_boolean compressed;

    compression_header_size = bfd_get_compression_header_size (abfd, sec);
    if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
        abort ();
    header_size = compression_header_size ? compression_header_size : 12;

    /* Don't decompress the section.  */
    sec->compress_status = COMPRESS_SECTION_NONE;

    /* Read the header.  */
    if (bfd_get_section_contents (abfd, sec, header, 0, header_size))
    {
        if (compression_header_size == 0)
            /* In this case, it should be "ZLIB" followed by the uncompressed
            section size, 8 bytes in big-endian order.  */
            compressed = CONST_STRNEQ ((char*) header , "ZLIB");
        else
            compressed = TRUE;
    }
    else
        compressed = FALSE;

    *uncompressed_size_p = sec->size;
    if (compressed)
    {
        if (compression_header_size != 0)
        {
            if (!bfd_check_compression_header (abfd, header, sec,
                                               uncompressed_size_p))
                compression_header_size = -1;
        }
        /* Check for the pathalogical case of a debug string section that
        contains the string ZLIB.... as the first entry.  We assume that
         no uncompressed .debug_str section would ever be big enough to
         have the first byte of its (big-endian) size be non-zero.  */
        else if (strcmp (sec->name, ".debug_str") == 0
                 && ISPRINT (header[4]))
            compressed = FALSE;
        else
            *uncompressed_size_p = bfd_getb64 (header + 4);
    }

    /* Restore compress_status.  */
    sec->compress_status = saved;
    *compression_header_size_p = compression_header_size;
    return compressed;
}
Example #14
0
static void secscan (bfd *file, sec_ptr sec, void *userParam)
{
    SecScanParam *param = (SecScanParam *) userParam;
    if (strcmp(param->sectionName, bfd_section_name (file, sec))==0)
    {
        bfd_size_type size = bfd_section_size (file, sec);
        void *data = (void *) param->result.reserve(size);
        bfd_get_section_contents(file, sec, data, 0, size);
    }
}
Example #15
0
static void
som_solib_create_inferior_hook (int from_tty)
{
  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
  struct minimal_symbol *msymbol;
  unsigned int dld_flags, status, have_endo;
  asection *shlib_info;
  char buf[4];
  CORE_ADDR anaddr;

  /* First, remove all the solib event breakpoints.  Their addresses
     may have changed since the last time we ran the program.  */
  remove_solib_event_breakpoints ();

  if (symfile_objfile == NULL)
    return;

  /* First see if the objfile was dynamically linked.  */
  shlib_info = bfd_get_section_by_name (symfile_objfile->obfd, "$SHLIB_INFO$");
  if (!shlib_info)
    return;

  /* It's got a $SHLIB_INFO$ section, make sure it's not empty.  */
  if (bfd_section_size (symfile_objfile->obfd, shlib_info) == 0)
    return;

  /* Read the DL header.  */
  bfd_get_section_contents (symfile_objfile->obfd, shlib_info,
			    (char *) &dl_header, 0, sizeof (dl_header));

  have_endo = 0;
  /* Slam the pid of the process into __d_pid.

     We used to warn when this failed, but that warning is only useful
     on very old HP systems (hpux9 and older).  The warnings are an
     annoyance to users of modern systems and foul up the testsuite as
     well.  As a result, the warnings have been disabled.  */
  msymbol = lookup_minimal_symbol ("__d_pid", NULL, symfile_objfile);
  if (msymbol == NULL)
    goto keep_going;

  anaddr = SYMBOL_VALUE_ADDRESS (msymbol);
  store_unsigned_integer (buf, 4, byte_order, PIDGET (inferior_ptid));
  status = target_write_memory (anaddr, buf, 4);
  if (status != 0)
    {
      warning (_("\
Unable to write __d_pid.\n\
Suggest linking with /opt/langtools/lib/end.o.\n\
GDB will be unable to track shl_load/shl_unload calls"));
      goto keep_going;
    }
Example #16
0
static bfd *find_debug_file(bfd *lib, const char *aFileName)
{
  // check for a separate debug file with symbols
  asection *sect = bfd_get_section_by_name(lib, ".gnu_debuglink");

  if (!sect)
    return nullptr;

  bfd_size_type debuglinkSize = bfd_section_size (objfile->obfd, sect);

  char *debuglink = new char[debuglinkSize];
  bfd_get_section_contents(lib, sect, debuglink, 0, debuglinkSize);

  // crc checksum is aligned to 4 bytes, and after the NUL.
  int crc_offset = (int(strlen(debuglink)) & ~3) + 4;
  unsigned long crc32 = bfd_get_32(lib, debuglink + crc_offset);

  // directory component
  char *dirbuf = strdup(aFileName);
  const char *dir = dirname(dirbuf);

  static const char debug_subdir[] = ".debug";
  // This is gdb's default global debugging info directory, but gdb can
  // be instructed to use a different directory.
  static const char global_debug_dir[] = "/usr/lib/debug";

  char *filename =
    new char[strlen(global_debug_dir) + strlen(dir) + crc_offset + 3];

  // /path/debuglink
  sprintf(filename, "%s/%s", dir, debuglink);
  bfd *debugFile = try_debug_file(filename, crc32);
  if (!debugFile) {

    // /path/.debug/debuglink
    sprintf(filename, "%s/%s/%s", dir, debug_subdir, debuglink);
    debugFile = try_debug_file(filename, crc32);
    if (!debugFile) {

      // /usr/lib/debug/path/debuglink
      sprintf(filename, "%s/%s/%s", global_debug_dir, dir, debuglink);
      debugFile = try_debug_file(filename, crc32);
    }
  }

  delete[] filename;
  free(dirbuf);
  delete[] debuglink;

  return debugFile;
}
Example #17
0
static void
core_process_module_section (bfd *abfd, asection *sect, void *obj)
{
  struct cpms_data *data = obj;
  enum bfd_endian byte_order = gdbarch_byte_order (data->gdbarch);

  char *module_name;
  size_t module_name_size;
  CORE_ADDR base_addr;

  char *buf = NULL;

  if (strncmp (sect->name, ".module", 7) != 0)
    return;

  buf = xmalloc (bfd_get_section_size (sect) + 1);
  if (!buf)
    {
      printf_unfiltered ("memory allocation failed for %s\n", sect->name);
      goto out;
    }
  if (!bfd_get_section_contents (abfd, sect,
				 buf, 0, bfd_get_section_size (sect)))
    goto out;



  /* A DWORD (data_type) followed by struct windows_core_module_info.  */

  base_addr =
    extract_unsigned_integer (buf + 4, 4, byte_order);

  module_name_size =
    extract_unsigned_integer (buf + 8, 4, byte_order);

  module_name = buf + 12;
  if (module_name - buf + module_name_size > bfd_get_section_size (sect))
    goto out;

  /* The first module is the .exe itself.  */
  if (data->module_count != 0)
    windows_xfer_shared_library (module_name, base_addr,
				 data->gdbarch, data->obstack);
  data->module_count++;

out:
  if (buf)
    xfree (buf);
  return;
}
Example #18
0
static void
initialize_sections(asm_program_t *prog, bfd_vma base)
{
  bfd_vma offset = 0;
  struct disassemble_info *disasm_info = &prog->disasm_info;
  assert(prog);
  bfd *abfd = prog->abfd;
  unsigned int opb = bfd_octets_per_byte(abfd);
  disasm_info->octets_per_byte = opb;
  init_disasm_info(prog);
  section_t **nextseg = &prog->segs;
  asection *section;

  /* Set to NULL in case there are zero segments. */
  *nextseg = NULL;

  /* Look for the section loaded into the lowest memory address */
  if (base != -1) {

    offset = base - asmir_get_base_address(prog);
    //fprintf(stderr, "Adjusting by %Lx\n", offset);
  }

  for(section = abfd->sections; section; section = section->next) {
    section_t *seg;
    bfd_byte *data;
    bfd_vma datasize = bfd_get_section_size_before_reloc(section);

    if(datasize == 0) continue;

    section->vma += offset;
    data = bfd_alloc2(abfd, datasize, sizeof(bfd_byte));
    bfd_get_section_contents(abfd, section, data, 0, datasize);
    seg = bfd_alloc(abfd, sizeof(section_t));
    seg->data = data;
    seg->datasize = datasize;
    seg->start_addr = section->vma;
    seg->end_addr = section->vma + datasize/opb;
    seg->section = section;
    seg->is_code = !!(section->flags & SEC_CODE);


    seg->next = NULL;
    *nextseg = seg;
    nextseg = &seg->next;
  }

}
Example #19
0
bfd_boolean
bfd_init_section_decompress_status (bfd *abfd, sec_ptr sec)
{
    bfd_byte header[MAX_COMPRESSION_HEADER_SIZE];
    int compression_header_size;
    int header_size;
    bfd_size_type uncompressed_size;

    compression_header_size = bfd_get_compression_header_size (abfd, sec);
    if (compression_header_size > MAX_COMPRESSION_HEADER_SIZE)
        abort ();
    header_size = compression_header_size ? compression_header_size : 12;

    /* Read the header.  */
    if (sec->rawsize != 0
            || sec->contents != NULL
            || sec->compress_status != COMPRESS_SECTION_NONE
            || !bfd_get_section_contents (abfd, sec, header, 0, header_size))
    {
        bfd_set_error (bfd_error_invalid_operation);
        return FALSE;
    }

    if (compression_header_size == 0)
    {
        /* In this case, it should be "ZLIB" followed by the uncompressed
        section size, 8 bytes in big-endian order.  */
        if (! CONST_STRNEQ ((char*) header, "ZLIB"))
        {
            bfd_set_error (bfd_error_wrong_format);
            return FALSE;
        }
        uncompressed_size = bfd_getb64 (header + 4);
    }
    else if (!bfd_check_compression_header (abfd, header, sec,
                                            &uncompressed_size))
    {
        bfd_set_error (bfd_error_wrong_format);
        return FALSE;
    }

    sec->compressed_size = sec->size;
    sec->size = uncompressed_size;
    sec->compress_status = DECOMPRESS_SECTION_SIZED;

    return TRUE;
}
Example #20
0
File: disasm.c Project: abrt/satyr
char *
sr_disasm_binary_to_text(struct sr_disasm_state *state,
                         uint64_t start_offset,
                         uint64_t size,
                         char **error_message)
{
#if HAVE_LIBOPCODES
    asection *section = state->info.section;
    if (start_offset < section->vma
        || (start_offset + size) > section->vma + section->size)
    {
        *error_message = sr_asprintf(
            "Invalid function range: 0x%"PRIx64" - 0x%"PRIx64,
            start_offset,
            start_offset + size);

        return NULL;
    }

    char *code = sr_malloc(size);
    bool success = bfd_get_section_contents(state->bfd_file,
                                            state->info.section,
                                            code,
                                            start_offset - section->vma,
                                            size);

    if (!success)
    {
        *error_message = sr_strdup("Failed to get section contents.");
        return NULL;
    }

    struct sr_strbuf *strbuf = sr_strbuf_new();
    for (int i = 0; i < size; ++i)
    {
        sr_strbuf_append_strf(strbuf, "0x%02x ", (unsigned)code[i]);
        if ((i + 1) % 12 == 0)
            sr_strbuf_append_char(strbuf, '\n');
    }

    free(code);
    return sr_strbuf_free_nobuf(strbuf);
#else // HAVE_LIBOPCODES
    *error_message = sr_asprintf("satyr compiled without libopcodes");
    return NULL;
#endif // HAVE_LIBOPCODES
}
Example #21
0
/* Open shared library BFD.  */
static bfd *
spu_bfd_open (char *pathname)
{
  char *original_name = strrchr (pathname, '@');
  bfd *abfd;
  asection *spu_name;
  unsigned long long addr;
  int fd;

  /* Handle regular SVR4 libraries.  */
  if (!original_name)
    return svr4_so_ops.bfd_open (pathname);

  /* Decode object ID.  */
  if (sscanf (original_name, "@0x%llx <%d>", &addr, &fd) != 2)
    internal_error (__FILE__, __LINE__, "bad object ID");

  /* Open BFD representing SPE executable.  */
  abfd = spu_bfd_fopen (original_name, (CORE_ADDR) addr);
  if (!abfd)
    error (_("Cannot read SPE executable at %s"), original_name);

  /* Retrieve SPU name note.  */
  spu_name = bfd_get_section_by_name (abfd, ".note.spu_name");
  if (spu_name)
    {
      int sect_size = bfd_section_size (abfd, spu_name);

      if (sect_size > 20)
	{
	  char *buf
	    = (char *) alloca (sect_size - 20 + strlen (original_name) + 1);

	  bfd_get_section_contents (abfd, spu_name, buf, 20, sect_size - 20);
	  buf[sect_size - 20] = '\0';

	  strcat (buf, original_name);

	  xfree ((char *)abfd->filename);
	  abfd->filename = xstrdup (buf);
	}
    }

  return abfd;
}
Example #22
0
CORE_ADDR
ppc64_convert_from_func_ptr_addr (struct gdbarch *gdbarch,
					CORE_ADDR addr,
					struct target_ops *targ)
{
  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  struct target_section *s = target_section_by_addr (targ, addr);

  /* Check if ADDR points to a function descriptor.  */
  if (s && strcmp (s->the_bfd_section->name, ".opd") == 0)
    {
      /* There may be relocations that need to be applied to the .opd 
	 section.  Unfortunately, this function may be called at a time
	 where these relocations have not yet been performed -- this can
	 happen for example shortly after a library has been loaded with
	 dlopen, but ld.so has not yet applied the relocations.

	 To cope with both the case where the relocation has been applied,
	 and the case where it has not yet been applied, we do *not* read
	 the (maybe) relocated value from target memory, but we instead
	 read the non-relocated value from the BFD, and apply the relocation
	 offset manually.

	 This makes the assumption that all .opd entries are always relocated
	 by the same offset the section itself was relocated.  This should
	 always be the case for GNU/Linux executables and shared libraries.
	 Note that other kind of object files (e.g. those added via
	 add-symbol-files) will currently never end up here anyway, as this
	 function accesses *target* sections only; only the main exec and
	 shared libraries are ever added to the target.  */

      gdb_byte buf[8];
      int res;

      res = bfd_get_section_contents (s->the_bfd_section->owner,
				      s->the_bfd_section,
				      &buf, addr - s->addr, 8);
      if (res != 0)
	return extract_unsigned_integer (buf, 8, byte_order)
		- bfd_section_vma (s->bfd, s->the_bfd_section) + s->addr;
   }

  return addr;
}
Example #23
0
static void
mygeneric_load (bfd *loadfile_bfd)
{
  asection *s;

  for (s = loadfile_bfd->sections; s; s = s->next)
    {
      if (s->flags & SEC_LOAD)
	{
	  bfd_size_type size;

	  size = bfd_get_section_size_before_reloc (s);
	  if (size > 0)
	    {
	      char *buffer;
	      bfd_vma lma;	/* use load address, not virtual address */

	      buffer = xmalloc (size);
	      lma = s->lma;

	      /* Is this really necessary? I guess it gives the user something
	       * to look at during a long download.
		   */
	      printf ("Loading section %s, size 0x%lx lma 0x%lx\n",
		      bfd_get_section_name (loadfile_bfd, s),
		      (unsigned long) size,
		      (unsigned long) lma);	/* chops high 32 bits.  FIXME!! */

	      bfd_get_section_contents (loadfile_bfd, s, buffer, 0, size);

	      write_inferior_memory (lma, buffer, size);
	      free (buffer);
	    }
	}
    }

  printf ("Start address 0x%lx\n",
	  (unsigned long) loadfile_bfd->start_address);

  /* We were doing this in remote-mips.c, I suspect it is right
   * for other targets too.
   */
  /* write_pc (loadfile_bfd->start_address); *//* FIXME!! */
}
Example #24
0
static VALUE cls_section_contents(VALUE instance) {
	/* lazy-loading of section list */
	VALUE var = rb_iv_get(instance, IVAR(SEC_ATTR_CONTENTS));
	if ( var == Qnil ) {
		unsigned char * buf;
		unsigned int size;
		asection * sec;

		var = Qnil;
		Data_Get_Struct(instance, asection, sec);
		size = bfd_section_size( sec->owner, sec );
		buf = calloc( size, 1 );
		if ( buf && 
		     bfd_get_section_contents(sec->owner, sec, buf, 0, size) ) {
			var = rb_str_new( (const char *) buf, size );
			rb_iv_set(instance, IVAR(SEC_ATTR_CONTENTS), var); 
		}
	}
	return var;
}
Example #25
0
static int
check_note (bfd *abfd, asection *sect, char *note, unsigned int *sectsize,
	    const char *name, unsigned long descsz, unsigned long type)
{
  unsigned long notesz;

  if (*sectsize)
    {
      if (!bfd_get_section_contents (abfd, sect, note, 0, *sectsize))
	return 0;
      *sectsize = 0;
    }

  /* Calculate the size of this note.  */
  notesz = strlen (name) + 1;
  notesz = ((notesz + 3) & ~3);
  notesz += descsz;
  notesz = ((notesz + 3) & ~3);

  /* If this assertion triggers, increase MAX_NOTESZ.  */
  gdb_assert (notesz <= MAX_NOTESZ);

  /* Check whether SECT is big enough to comtain the complete note.  */
  if (notesz > bfd_section_size (abfd, sect))
    return 0;

  /* Check the note name.  */
  if (bfd_h_get_32 (abfd, note) != (strlen (name) + 1)
      || strcmp (note + 12, name) != 0)
    return 0;

  /* Check the descriptor size.  */
  if (bfd_h_get_32 (abfd, note + 4) != descsz)
    return 0;

  /* Check the note type.  */
  if (bfd_h_get_32 (abfd, note + 8) != type)
    return 0;

  return 1;
}
Example #26
0
/// Examines the bfd in order to find the sections containing data to be loaded.
/// Also fills the program_data array.
void trap::ExecLoader::load_program_data() {
  bfd_section* p = NULL;
  std::map<unsigned long, unsigned char> mem_map;
  for (p = this->exec_image->sections; p != NULL; p = p->next) {
    flagword flags = bfd_get_section_flags(this->exec_image, p);
    if ((flags & SEC_ALLOC) != 0 && (flags & SEC_DEBUGGING) == 0 && (flags & SEC_THREAD_LOCAL) == 0) {
      //Ok, this is a section which must be in the final executable;
      //Lets see if it has content: if not I have to pad the section with zeros,
      //otherwise I load it
      bfd_size_type datasize = bfd_section_size(this->exec_image, p);
      bfd_vma vma = bfd_get_section_vma(this->exec_image, p);
      std::map<unsigned long, unsigned char>::iterator mem_it = mem_map.begin();
      if ((flags & SEC_HAS_CONTENTS) != 0) {
/*#ifndef NDEBUG
        std::cerr << "Loading data fom section " << p->name << " Start Address " << std::showbase << std::hex << vma << " Size " << std::hex << datasize << " End Address " << std::hex << datasize + vma << std::dec << " Swap Endianess " << swap_endianess << " flags " << std::hex << std::showbase << flags << std::dec << std::endl;
#endif*/
        bfd_byte* data = new bfd_byte[datasize];
        bfd_get_section_contents (this->exec_image, p, data, 0, datasize);
        for (unsigned i = 0; i < datasize; i++) {
          mem_it = mem_map.insert(mem_it, std::pair<unsigned long, unsigned char>(vma + i, data[i]));
        }
        delete [] data;
      } else {
/*#ifndef NDEBUG
        std::cerr << "Filling with 0s section " << p->name << " Start Address " << std::showbase << std::hex << vma << " Size " << std::hex << datasize << " End Address " << std::hex << datasize + vma << std::dec << std::endl;
#endif*/
        for (unsigned i = 0; i < datasize; i++)
          mem_it = mem_map.insert(mem_it, std::pair<unsigned long, unsigned char>(vma + i, 0));
      }
    }
  }
  //ok,  I now have all the map of the memory; I simply have to fill in the
  //this->program_data  array
  this->data_start = mem_map.begin()->first;
  this->program_dim = mem_map.rbegin()->first - this->data_start + 1;
  this->program_data = new unsigned char[this->program_dim];
  std::map<unsigned long, unsigned char>::iterator mem_it,  mem_end;
  for (mem_it = mem_map.begin(),  mem_end = mem_map.end(); mem_it != mem_end; mem_it++) {
    this->program_data[mem_it->first - this->data_start] = mem_it->second;
  }
} // ExecLoader::load_program_data()
LONGEST
target_bfd_xfer_partial (struct target_ops *ops,
			 enum target_object object,
			 const char *annex, gdb_byte *readbuf,
			 const gdb_byte *writebuf,
			 ULONGEST offset, LONGEST len)
{
  switch (object)
    {
    case TARGET_OBJECT_MEMORY:
      {
	struct section_table *s = target_section_by_addr (ops, offset);
	if (s == NULL)
	  return -1;
	/* If the length extends beyond the section, truncate it.  Be
           careful to not suffer from overflow (wish S contained a
           length).  */
	if ((offset - s->addr + len) > (s->endaddr - s->addr))
	  len = (s->endaddr - s->addr) - (offset - s->addr);
	if (readbuf != NULL
	    && !bfd_get_section_contents (s->bfd, s->the_bfd_section,
					  readbuf, offset - s->addr, len))
	  return -1;
#if 1
	if (writebuf != NULL)
	  return -1;
#else
	/* FIXME: cagney/2003-10-31: The BFD interface doesn't yet
           take a const buffer.  */
	if (writebuf != NULL
	    && !bfd_set_section_contents (s->bfd, s->the_bfd_section,
					  writebuf, offset - s->addr, len))
	  return -1;
#endif
	return len;
      }
    default:
      return -1;
    }
}
Example #28
0
static char * 
fbsd_thread_get_name (lwpid_t lwpid)
{
  static char last_thr_name[MAXCOMLEN + 1];
  char section_name[32];
  struct ptrace_lwpinfo lwpinfo;
  bfd_size_type size;
  struct bfd_section *section;

  if (target_has_execution)
    {
      if (ptrace (PT_LWPINFO, lwpid, (caddr_t)&lwpinfo, sizeof (lwpinfo)) == -1)
        goto fail;
      strncpy (last_thr_name, lwpinfo.pl_tdname, sizeof (last_thr_name) - 1);
    }
  else
    {
      snprintf (section_name, sizeof (section_name), ".tname/%u", lwpid);
      section = bfd_get_section_by_name (core_bfd, section_name);
      if (! section)
        goto fail;

      /* Section size fix-up. */
      size = bfd_section_size (core_bfd, section);
      if (size > sizeof (last_thr_name))
        size = sizeof (last_thr_name);

      if (! bfd_get_section_contents (core_bfd, section, last_thr_name,
	       (file_ptr)0, size))
        goto fail;
      if (last_thr_name[0] == '\0')
        goto fail;
    }
    last_thr_name[sizeof (last_thr_name) - 1] = '\0';
    return last_thr_name;
fail:
     strcpy (last_thr_name, "<unknown>");
     return last_thr_name;
}
Example #29
0
void
core_get_text_space (bfd *cbfd)
{
  core_text_space = malloc (bfd_get_section_size (core_text_sect));

  if (!core_text_space)
    {
      fprintf (stderr, _("%s: ran out room for %lu bytes of text space\n"),
	       whoami, (unsigned long) bfd_get_section_size (core_text_sect));
      done (1);
    }

  if (!bfd_get_section_contents (cbfd, core_text_sect, core_text_space,
				 0, bfd_get_section_size (core_text_sect)))
    {
      bfd_perror ("bfd_get_section_contents");
      free (core_text_space);
      core_text_space = 0;
    }

  if (!core_text_space)
    fprintf (stderr, _("%s: can't do -c\n"), whoami);
}
Example #30
0
File: corelow.c Project: Xilinx/gdb
static LONGEST
get_core_siginfo (bfd *abfd, gdb_byte *readbuf, ULONGEST offset, LONGEST len)
{
  asection *section;
  char *section_name;
  const char *name = ".note.linuxcore.siginfo";

  if (ptid_get_lwp (inferior_ptid))
    section_name = xstrprintf ("%s/%ld", name,
			       ptid_get_lwp (inferior_ptid));
  else
    section_name = xstrdup (name);

  section = bfd_get_section_by_name (abfd, section_name);
  xfree (section_name);
  if (section == NULL)
    return -1;

  if (!bfd_get_section_contents (abfd, section, readbuf, offset, len))
    return -1;

  return len;
}