Example #1
0
static void mod_dumpinitializer(mod_initializer_t initializer,
                                FAR struct mod_loadinfo_s *loadinfo)
{
  mod_dumpbuffer("Initializer code", (FAR const uint8_t *)initializer,
                 MIN(loadinfo->textsize - loadinfo->ehdr.e_entry, 512));
}
Example #2
0
int mod_initialize(FAR const char *filename,
                   FAR struct mod_loadinfo_s *loadinfo)
{
  int ret;

  svdbg("filename: %s loadinfo: %p\n", filename, loadinfo);

  /* Clear the load info structure */

  memset(loadinfo, 0, sizeof(struct mod_loadinfo_s));

  /* Get the length of the file. */

  ret = mod_filelen(loadinfo, filename);
  if (ret < 0)
    {
      sdbg("mod_filelen failed: %d\n", ret);
      return ret;
    }

  /* Open the binary file for reading (only) */

  loadinfo->filfd = open(filename, O_RDONLY);
  if (loadinfo->filfd < 0)
    {
      int errval = errno;
      sdbg("Failed to open ELF binary %s: %d\n", filename, errval);
      return -errval;
    }

  /* Read the ELF ehdr from offset 0 */

  ret = mod_read(loadinfo, (FAR uint8_t *)&loadinfo->ehdr,
                    sizeof(Elf32_Ehdr), 0);
  if (ret < 0)
    {
      sdbg("Failed to read ELF header: %d\n", ret);
      return ret;
    }

  mod_dumpbuffer("ELF header", (FAR const uint8_t *)&loadinfo->ehdr,
                    sizeof(Elf32_Ehdr));

  /* Verify the ELF header */

  ret = mod_verifyheader(&loadinfo->ehdr);
  if (ret < 0)
    {
      /* This may not be an error because we will be called to attempt loading
       * EVERY binary.  If mod_verifyheader() does not recognize the ELF header,
       * it will -ENOEXEC whcih simply informs the system that the file is not an
       * ELF file.  mod_verifyheader() will return other errors if the ELF header
       * is not correctly formed.
       */

      sdbg("Bad ELF header: %d\n", ret);
      return ret;
    }

  return OK;
}