pexxigen.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,044 行 · 第 1/5 页

C
2,044
字号
  filehdr_in->pe.dos_message[2]  = 0x4c01b821;  filehdr_in->pe.dos_message[3]  = 0x685421cd;  filehdr_in->pe.dos_message[4]  = 0x70207369;  filehdr_in->pe.dos_message[5]  = 0x72676f72;  filehdr_in->pe.dos_message[6]  = 0x63206d61;  filehdr_in->pe.dos_message[7]  = 0x6f6e6e61;  filehdr_in->pe.dos_message[8]  = 0x65622074;  filehdr_in->pe.dos_message[9]  = 0x6e757220;  filehdr_in->pe.dos_message[10] = 0x206e6920;  filehdr_in->pe.dos_message[11] = 0x20534f44;  filehdr_in->pe.dos_message[12] = 0x65646f6d;  filehdr_in->pe.dos_message[13] = 0x0a0d0d2e;  filehdr_in->pe.dos_message[14] = 0x24;  filehdr_in->pe.dos_message[15] = 0x0;  filehdr_in->pe.nt_signature = NT_SIGNATURE;  bfd_h_put_16 (abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);  bfd_h_put_16 (abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);  bfd_h_put_32 (abfd, time (0), (bfd_byte *) filehdr_out->f_timdat);  PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,		      (bfd_byte *) filehdr_out->f_symptr);  bfd_h_put_32 (abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);  bfd_h_put_16 (abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);  bfd_h_put_16 (abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);  /* put in extra dos header stuff.  This data remains essentially     constant, it just has to be tacked on to the beginning of all exes     for NT */  bfd_h_put_16 (abfd, filehdr_in->pe.e_magic, (bfd_byte *) filehdr_out->e_magic);  bfd_h_put_16 (abfd, filehdr_in->pe.e_cblp, (bfd_byte *) filehdr_out->e_cblp);  bfd_h_put_16 (abfd, filehdr_in->pe.e_cp, (bfd_byte *) filehdr_out->e_cp);  bfd_h_put_16 (abfd, filehdr_in->pe.e_crlc, (bfd_byte *) filehdr_out->e_crlc);  bfd_h_put_16 (abfd, filehdr_in->pe.e_cparhdr,	       (bfd_byte *) filehdr_out->e_cparhdr);  bfd_h_put_16 (abfd, filehdr_in->pe.e_minalloc,	       (bfd_byte *) filehdr_out->e_minalloc);  bfd_h_put_16 (abfd, filehdr_in->pe.e_maxalloc,	       (bfd_byte *) filehdr_out->e_maxalloc);  bfd_h_put_16 (abfd, filehdr_in->pe.e_ss, (bfd_byte *) filehdr_out->e_ss);  bfd_h_put_16 (abfd, filehdr_in->pe.e_sp, (bfd_byte *) filehdr_out->e_sp);  bfd_h_put_16 (abfd, filehdr_in->pe.e_csum, (bfd_byte *) filehdr_out->e_csum);  bfd_h_put_16 (abfd, filehdr_in->pe.e_ip, (bfd_byte *) filehdr_out->e_ip);  bfd_h_put_16 (abfd, filehdr_in->pe.e_cs, (bfd_byte *) filehdr_out->e_cs);  bfd_h_put_16 (abfd, filehdr_in->pe.e_lfarlc, (bfd_byte *) filehdr_out->e_lfarlc);  bfd_h_put_16 (abfd, filehdr_in->pe.e_ovno, (bfd_byte *) filehdr_out->e_ovno);  {    int idx;    for (idx = 0; idx < 4; idx++)      bfd_h_put_16 (abfd, filehdr_in->pe.e_res[idx],		    (bfd_byte *) filehdr_out->e_res[idx]);  }  bfd_h_put_16 (abfd, filehdr_in->pe.e_oemid, (bfd_byte *) filehdr_out->e_oemid);  bfd_h_put_16 (abfd, filehdr_in->pe.e_oeminfo,		(bfd_byte *) filehdr_out->e_oeminfo);  {    int idx;    for (idx = 0; idx < 10; idx++)      bfd_h_put_16 (abfd, filehdr_in->pe.e_res2[idx],		    (bfd_byte *) filehdr_out->e_res2[idx]);  }  bfd_h_put_32 (abfd, filehdr_in->pe.e_lfanew, (bfd_byte *) filehdr_out->e_lfanew);  {    int idx;    for (idx = 0; idx < 16; idx++)      bfd_h_put_32 (abfd, filehdr_in->pe.dos_message[idx],		    (bfd_byte *) filehdr_out->dos_message[idx]);  }  /* Also put in the NT signature.  */  bfd_h_put_32 (abfd, filehdr_in->pe.nt_signature,		(bfd_byte *) filehdr_out->nt_signature);  return FILHSZ;}unsigned int_bfd_XX_only_swap_filehdr_out (abfd, in, out)     bfd       *abfd;     PTR	in;     PTR	out;{  struct internal_filehdr *filehdr_in = (struct internal_filehdr *) in;  FILHDR *filehdr_out = (FILHDR *) out;  bfd_h_put_16 (abfd, filehdr_in->f_magic, (bfd_byte *) filehdr_out->f_magic);  bfd_h_put_16 (abfd, filehdr_in->f_nscns, (bfd_byte *) filehdr_out->f_nscns);  bfd_h_put_32 (abfd, filehdr_in->f_timdat, (bfd_byte *) filehdr_out->f_timdat);  PUT_FILEHDR_SYMPTR (abfd, (bfd_vma) filehdr_in->f_symptr,		      (bfd_byte *) filehdr_out->f_symptr);  bfd_h_put_32 (abfd, filehdr_in->f_nsyms, (bfd_byte *) filehdr_out->f_nsyms);  bfd_h_put_16 (abfd, filehdr_in->f_opthdr, (bfd_byte *) filehdr_out->f_opthdr);  bfd_h_put_16 (abfd, filehdr_in->f_flags, (bfd_byte *) filehdr_out->f_flags);  return FILHSZ;}unsigned int_bfd_XXi_swap_scnhdr_out (abfd, in, out)     bfd       *abfd;     PTR	in;     PTR	out;{  struct internal_scnhdr *scnhdr_int = (struct internal_scnhdr *) in;  SCNHDR *scnhdr_ext = (SCNHDR *) out;  unsigned int ret = SCNHSZ;  bfd_vma ps;  bfd_vma ss;  memcpy (scnhdr_ext->s_name, scnhdr_int->s_name, sizeof (scnhdr_int->s_name));  PUT_SCNHDR_VADDR (abfd,		    ((scnhdr_int->s_vaddr		      - pe_data (abfd)->pe_opthdr.ImageBase)		     & 0xffffffff),		    (bfd_byte *) scnhdr_ext->s_vaddr);  /* NT wants the size data to be rounded up to the next     NT_FILE_ALIGNMENT, but zero if it has no content (as in .bss,     sometimes).  */  if ((scnhdr_int->s_flags & IMAGE_SCN_CNT_UNINITIALIZED_DATA) != 0)    {      ps = scnhdr_int->s_size;      ss = 0;    }  else    {      ps = scnhdr_int->s_paddr;      ss = scnhdr_int->s_size;    }  PUT_SCNHDR_SIZE (abfd, ss,		   (bfd_byte *) scnhdr_ext->s_size);  /* s_paddr in PE is really the virtual size.  */  PUT_SCNHDR_PADDR (abfd, ps, (bfd_byte *) scnhdr_ext->s_paddr);  PUT_SCNHDR_SCNPTR (abfd, scnhdr_int->s_scnptr,		     (bfd_byte *) scnhdr_ext->s_scnptr);  PUT_SCNHDR_RELPTR (abfd, scnhdr_int->s_relptr,		     (bfd_byte *) scnhdr_ext->s_relptr);  PUT_SCNHDR_LNNOPTR (abfd, scnhdr_int->s_lnnoptr,		      (bfd_byte *) scnhdr_ext->s_lnnoptr);  /* Extra flags must be set when dealing with NT.  All sections should also     have the IMAGE_SCN_MEM_READ (0x40000000) flag set.  In addition, the     .text section must have IMAGE_SCN_MEM_EXECUTE (0x20000000) and the data     sections (.idata, .data, .bss, .CRT) must have IMAGE_SCN_MEM_WRITE set     (this is especially important when dealing with the .idata section since     the addresses for routines from .dlls must be overwritten).  If .reloc     section data is ever generated, we must add IMAGE_SCN_MEM_DISCARDABLE     (0x02000000).  Also, the resource data should also be read and     writable.  */  /* FIXME: alignment is also encoded in this field, at least on ppc (krk) */  /* FIXME: even worse, I don't see how to get the original alignment field*/  /*        back...                                                        */  {    int flags = scnhdr_int->s_flags;    bfd_h_put_32 (abfd, flags, (bfd_byte *) scnhdr_ext->s_flags);  }  if (coff_data (abfd)->link_info      && ! coff_data (abfd)->link_info->relocateable      && ! coff_data (abfd)->link_info->shared      && strcmp (scnhdr_int->s_name, ".text") == 0)    {      /* By inference from looking at MS output, the 32 bit field	 which is the combintion of the number_of_relocs and	 number_of_linenos is used for the line number count in	 executables.  A 16-bit field won't do for cc1.  The MS	 document says that the number of relocs is zero for	 executables, but the 17-th bit has been observed to be there.	 Overflow is not an issue: a 4G-line program will overflow a	 bunch of other fields long before this!  */      bfd_h_put_16 (abfd, scnhdr_int->s_nlnno & 0xffff,		    (bfd_byte *) scnhdr_ext->s_nlnno);      bfd_h_put_16 (abfd, scnhdr_int->s_nlnno >> 16,		    (bfd_byte *) scnhdr_ext->s_nreloc);    }  else    {      if (scnhdr_int->s_nlnno <= 0xffff)	bfd_h_put_16 (abfd, scnhdr_int->s_nlnno,		      (bfd_byte *) scnhdr_ext->s_nlnno);      else	{	  (*_bfd_error_handler) (_("%s: line number overflow: 0x%lx > 0xffff"),				 bfd_get_filename (abfd),				 scnhdr_int->s_nlnno);	  bfd_set_error (bfd_error_file_truncated);	  bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nlnno);	  ret = 0;	}      if (scnhdr_int->s_nreloc <= 0xffff)	bfd_h_put_16 (abfd, scnhdr_int->s_nreloc,		      (bfd_byte *) scnhdr_ext->s_nreloc);      else	{	  /* PE can deal with large #s of relocs, but not here */	  bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);	  scnhdr_int->s_flags |= IMAGE_SCN_LNK_NRELOC_OVFL;	  bfd_h_put_32 (abfd, scnhdr_int->s_flags,			(bfd_byte *) scnhdr_ext->s_flags);#if 0	  (*_bfd_error_handler) (_("%s: reloc overflow 1: 0x%lx > 0xffff"),				 bfd_get_filename (abfd),				 scnhdr_int->s_nreloc);	  bfd_set_error (bfd_error_file_truncated);	  bfd_h_put_16 (abfd, 0xffff, (bfd_byte *) scnhdr_ext->s_nreloc);	  ret = 0;#endif	}    }  return ret;}static char * dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] = {  N_("Export Directory [.edata (or where ever we found it)]"),  N_("Import Directory [parts of .idata]"),  N_("Resource Directory [.rsrc]"),  N_("Exception Directory [.pdata]"),  N_("Security Directory"),  N_("Base Relocation Directory [.reloc]"),  N_("Debug Directory"),  N_("Description Directory"),  N_("Special Directory"),  N_("Thread Storage Directory [.tls]"),  N_("Load Configuration Directory"),  N_("Bound Import Directory"),  N_("Import Address Table Directory"),  N_("Delay Import Directory"),  N_("Reserved"),  N_("Reserved")};/**********************************************************************/#ifdef POWERPC_LE_PE/* The code for the PPC really falls in the "architecture dependent"   category.  However, it's not clear that anyone will ever care, so   we're ignoring the issue for now; if/when PPC matters, some of this   may need to go into peicode.h, or arguments passed to enable the   PPC- specific code.  */#endif/**********************************************************************/static booleanpe_print_idata (abfd, vfile)     bfd *abfd;     PTR vfile;{  FILE *file = (FILE *) vfile;  bfd_byte *data;  asection *section;  bfd_signed_vma adj;#ifdef POWERPC_LE_PE  asection *rel_section = bfd_get_section_by_name (abfd, ".reldata");#endif  bfd_size_type datasize = 0;  bfd_size_type dataoff;  bfd_size_type i;  int onaline = 20;  pe_data_type *pe = pe_data (abfd);  struct internal_extra_pe_aouthdr *extra = &pe->pe_opthdr;  bfd_vma addr;  addr = extra->DataDirectory[1].VirtualAddress;  if (addr == 0 && extra->DataDirectory[1].Size == 0)    {      /* Maybe the extra header isn't there.  Look for the section.  */      section = bfd_get_section_by_name (abfd, ".idata");      if (section == NULL)	return true;      addr = section->vma;      datasize = bfd_section_size (abfd, section);      if (datasize == 0)	return true;    }  else    {      addr += extra->ImageBase;      for (section = abfd->sections; section != NULL; section = section->next)	{	  datasize = bfd_section_size (abfd, section);	  if (addr >= section->vma && addr < section->vma + datasize)	    break;	}      if (section == NULL)	{	  fprintf (file,		   _("\nThere is an import table, but the section containing it could not be found\n"));	  return true;	}    }  fprintf (file, _("\nThere is an import table in %s at 0x%lx\n"),	   section->name, (unsigned long) addr);  dataoff = addr - section->vma;  datasize -= dataoff;#ifdef POWERPC_LE_PE  if (rel_section != 0 && bfd_section_size (abfd, rel_section) != 0)    {      /* The toc address can be found by taking the starting address,	 which on the PPC locates a function descriptor. The	 descriptor consists of the function code starting address	 followed by the address of the toc. The starting address we	 get from the bfd, and the descriptor is supposed to be in the	 .reldata section.  */      bfd_vma loadable_toc_address;      bfd_vma toc_address;      bfd_vma start_address;      bfd_byte *data = 0;      int offset;      data = (bfd_byte *) bfd_malloc ((size_t) bfd_section_size (abfd,								 rel_section));      if (data == NULL && bfd_section_size (abfd, rel_section) != 0)	return false;      bfd_get_section_contents (abfd,				rel_section,				(PTR) data, 0,				bfd_section_size (abfd, rel_section));      offset = abfd->start_address - rel_section->vma;      start_address = bfd_get_32 (abfd, data + offset);      loadable_toc_address = bfd_get_32 (abfd, data + offset + 4);      toc_address = loadable_toc_address - 32768;      fprintf (file,	       _("\nFunction descriptor located at the start address: %04lx\n"),	       (unsigned long int) (abfd->start_address));      fprintf (file,	       _("\tcode-base %08lx toc (loadable/actual) %08lx/%08lx\n"),	       start_address, loadable_toc_address, toc_address);    }  else    {      fprintf (file,	       _("\nNo reldata section! Function descriptor not decoded.\n"));    }#endif  fprintf (file,	   _("\nThe Import Tables (interpreted %s section contents)\n"),	   section->name);  fprintf (file,	   _(" vma:            Hint    Time      Forward  DLL       First\n"));  fprintf (file,	   _("                 Table   Stamp     Chain    Name      Thunk\n"));  data = (bfd_byte *) bfd_malloc (dataoff + datasize);  if (data == NULL)    return false;  /* Read the whole section.  Some of the fields might be before dataoff.  */  if (! bfd_get_section_contents (abfd, section, (PTR) data,				  0, dataoff + datasize))    return false;  adj = section->vma - extra->ImageBase;  for (i = 0; i < datasize; i += onaline)    {      bfd_vma hint_addr;      bfd_vma time_stamp;      bfd_vma forward_chain;      bfd_vma dll_name;      bfd_vma first_thunk;      int idx = 0;      bfd_size_type j;      char *dll;      /* print (i + extra->DataDirectory[1].VirtualAddress)  */      fprintf (file, " %08lx\t", (unsigned long) (i + adj + dataoff));      if (i + 20 > datasize)	{	  /* Check stuff.  */	  ;	}      hint_addr = bfd_get_32 (abfd, data + i + dataoff);      time_stamp = bfd_get_32 (abfd, data + i + 4 + dataoff);      forward_chain = bfd_get_32 (abfd, data + i + 8 + dataoff);      dll_name = bfd_get_32 (abfd, data + i + 12 + dataoff);      first_thunk = bfd_get_32 (abfd, data + i + 16 + dataoff);      fprintf (file, "%08lx %08lx %08lx %08lx %08lx\n",	       (unsigned long) hint_addr,	       (unsigned long) time_stamp,	       (unsigned long) forward_chain,	       (unsigned long) dll_name,	       (unsigned long) first_thunk);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?