peicode.h
来自「基于4个mips核的noc设计」· C头文件 代码 · 共 1,324 行 · 第 1/3 页
H
1,324 行
created for us by the linker script. */ id4 = pe_ILF_make_a_section (& vars, ".idata$4", SIZEOF_IDATA4, 0); id5 = pe_ILF_make_a_section (& vars, ".idata$5", SIZEOF_IDATA5, 0); if (id4 == NULL || id5 == NULL) return false; /* Fill in the contents of these sections. */ if (import_name_type == IMPORT_ORDINAL) { if (ordinal == 0) /* XXX - treat as IMPORT_NAME ??? */ abort (); * (unsigned int *) id4->contents = ordinal | 0x80000000; * (unsigned int *) id5->contents = ordinal | 0x80000000; } else { char * symbol; /* Create .idata$6 - the Hint Name Table. */ id6 = pe_ILF_make_a_section (& vars, ".idata$6", SIZEOF_IDATA6, 0); if (id6 == NULL) return false; /* If necessary, trim the import symbol name. */ symbol = symbol_name; if (import_name_type != IMPORT_NAME) /* Skip any prefix in symbol_name. */ while (*symbol == '@' || * symbol == '?' || * symbol == '_') ++ symbol; if (import_name_type == IMPORT_NAME_UNDECORATE) { /* Truncate at the first '@' */ while (* symbol != 0 && * symbol != '@') symbol ++; * symbol = 0; } id6->contents[0] = ordinal & 0xff; id6->contents[1] = ordinal >> 8; strcpy (id6->contents + 2, symbol); } if (import_name_type != IMPORT_ORDINAL) { pe_ILF_make_a_reloc (& vars, 0, BFD_RELOC_RVA, id6); pe_ILF_save_relocs (& vars, id4); pe_ILF_make_a_reloc (& vars, 0, BFD_RELOC_RVA, id6); pe_ILF_save_relocs (& vars, id5); } /* Create extra sections depending upon the type of import we are dealing with. */ switch (import_type) { int i; case IMPORT_CODE: /* Create a .text section. First we need to look up its contents in the jump table. */ for (i = NUM_ENTRIES (jtab); i--;) { if (jtab[i].size == 0) continue; if (jtab[i].magic == magic) break; } /* If we did not find a matching entry something is wrong. */ if (i < 0) abort (); /* Create the .text section. */ text = pe_ILF_make_a_section (& vars, ".text", jtab[i].size, SEC_CODE); if (text == NULL) return false; /* Copy in the jump code. */ memcpy (text->contents, jtab[i].data, jtab[i].size); /* Create an import symbol. */ pe_ILF_make_a_symbol (& vars, "__imp_", symbol_name, id5, 0); imp_sym = vars.sym_ptr_ptr - 1; imp_index = vars.sym_index - 1; /* Create a reloc for the data in the text section. */#ifdef MIPS_ARCH_MAGIC_WINCE if (magic == MIPS_ARCH_MAGIC_WINCE) { pe_ILF_make_a_symbol_reloc (& vars, 0, BFD_RELOC_HI16_S, (struct symbol_cache_entry **) imp_sym, imp_index); pe_ILF_make_a_reloc (& vars, 0, BFD_RELOC_LO16, text); pe_ILF_make_a_symbol_reloc (& vars, 4, BFD_RELOC_LO16, (struct symbol_cache_entry **) imp_sym, imp_index); } else#endif pe_ILF_make_a_symbol_reloc (& vars, jtab[i].offset, BFD_RELOC_32, (asymbol **) imp_sym, imp_index); pe_ILF_save_relocs (& vars, text); break; case IMPORT_DATA: break; default: /* XXX code not yet written. */ abort (); } /* Initialise the bfd. */ memset (& internal_f, 0, sizeof (internal_f)); internal_f.f_magic = magic; internal_f.f_symptr = 0; internal_f.f_nsyms = 0; internal_f.f_flags = F_AR32WR | F_LNNO; /* XXX is this correct ? */ if ( ! bfd_set_start_address (abfd, 0) || ! bfd_coff_set_arch_mach_hook (abfd, & internal_f)) return false; if (bfd_coff_mkobject_hook (abfd, (PTR) & internal_f, NULL) == NULL) return false; coff_data (abfd)->pe = 1;#ifdef THUMBPEMAGIC if (vars.magic == THUMBPEMAGIC) /* Stop some linker warnings about thumb code not supporting interworking. */ coff_data (abfd)->flags |= F_INTERWORK | F_INTERWORK_SET;#endif /* Switch from file contents to memory contents. */ bfd_cache_close (abfd); abfd->iostream = (PTR) vars.bim; abfd->flags |= BFD_IN_MEMORY /* | HAS_LOCALS */; abfd->where = 0; obj_sym_filepos (abfd) = 0; /* Now create a symbol describing the imported value. */ switch (import_type) { bfd_byte * ptr; case IMPORT_CODE: pe_ILF_make_a_symbol (& vars, "", symbol_name, text, BSF_NOT_AT_END | BSF_FUNCTION); /* Create an import symbol for the DLL, without the .dll suffix. */ ptr = strrchr (source_dll, '.'); if (ptr) * ptr = 0; pe_ILF_make_a_symbol (& vars, "__IMPORT_DESCRIPTOR_", source_dll, NULL, 0); if (ptr) * ptr = '.'; break; case IMPORT_DATA: /* Nothing to do here. */ break; default: /* XXX code not yet written. */ abort (); } /* Point the bfd at the symbol table. */ obj_symbols (abfd) = vars.sym_cache; bfd_get_symcount (abfd) = vars.sym_index; obj_raw_syments (abfd) = vars.native_syms; obj_raw_syment_count (abfd) = vars.sym_index; obj_coff_external_syms (abfd) = (PTR) vars.esym_table; obj_coff_keep_syms (abfd) = true; obj_convert (abfd) = vars.sym_table; obj_conv_table_size (abfd) = vars.sym_index; obj_coff_strings (abfd) = vars.string_table; obj_coff_keep_strings (abfd) = true; abfd->flags |= HAS_SYMS; return true;}/* We have detected a Image Library Format archive element. Decode the element and return the appropriate target. */static const bfd_target *pe_ILF_object_p (bfd * abfd){ bfd_byte buffer[16]; bfd_byte * ptr; bfd_byte * symbol_name; bfd_byte * source_dll; unsigned int machine; unsigned long size; unsigned int ordinal; unsigned int types; unsigned short magic; /* Upon entry the first four buyes of the ILF header have already been read. Now read the rest of the header. */ if (bfd_read (buffer, 1, 16, abfd) != 16) return NULL; ptr = buffer; /* We do not bother to check the version number. version = bfd_h_get_16 (abfd, ptr); */ ptr += 2; machine = bfd_h_get_16 (abfd, ptr); ptr += 2; /* Check that the machine type is recognised. */ magic = 0; switch (machine) { case IMAGE_FILE_MACHINE_UNKNOWN: case IMAGE_FILE_MACHINE_ALPHA: case IMAGE_FILE_MACHINE_ALPHA64: case IMAGE_FILE_MACHINE_IA64: break; case IMAGE_FILE_MACHINE_I386:#ifdef I386MAGIC magic = I386MAGIC;#endif break; case IMAGE_FILE_MACHINE_M68K:#ifdef MC68AGIC magic = MC68MAGIC;#endif break; case IMAGE_FILE_MACHINE_R3000: case IMAGE_FILE_MACHINE_R4000: case IMAGE_FILE_MACHINE_R10000: case IMAGE_FILE_MACHINE_MIPS16: case IMAGE_FILE_MACHINE_MIPSFPU: case IMAGE_FILE_MACHINE_MIPSFPU16:#ifdef MIPS_ARCH_MAGIC_WINCE magic = MIPS_ARCH_MAGIC_WINCE;#endif break; case IMAGE_FILE_MACHINE_SH3: case IMAGE_FILE_MACHINE_SH4:#ifdef SH_ARCH_MAGIC_WINCE magic = SH_ARCH_MAGIC_WINCE;#endif break; case IMAGE_FILE_MACHINE_ARM:#ifdef ARMPEMAGIC magic = ARMPEMAGIC;#endif break; case IMAGE_FILE_MACHINE_THUMB:#ifdef THUMBPEMAGIC { extern const bfd_target TARGET_LITTLE_SYM; if (abfd->xvec == & TARGET_LITTLE_SYM) magic = THUMBPEMAGIC; }#endif break; case IMAGE_FILE_MACHINE_POWERPC: /* We no longer support PowerPC. */ default: _bfd_error_handler (_("%s: Unrecognised machine type (0x%x) in Import Library Format archive"), bfd_get_filename (abfd), machine); bfd_set_error (bfd_error_malformed_archive); return NULL; break; } if (magic == 0) { _bfd_error_handler (_("%s: Recognised but unhandled machine type (0x%x) in Import Library Format archive"), bfd_get_filename (abfd), machine); bfd_set_error (bfd_error_wrong_format); return NULL; } /* We do not bother to check the date. date = bfd_h_get_32 (abfd, ptr); */ ptr += 4; size = bfd_h_get_32 (abfd, ptr); ptr += 4; if (size == 0) { _bfd_error_handler (_("%s: size field is zero in Import Library Format header"), bfd_get_filename (abfd)); bfd_set_error (bfd_error_malformed_archive); return NULL; } ordinal = bfd_h_get_16 (abfd, ptr); ptr += 2; types = bfd_h_get_16 (abfd, ptr); /* ptr += 2; */ /* Now read in the two strings that follow. */ ptr = bfd_alloc (abfd, size); if (ptr == NULL) return NULL; if (bfd_read (ptr, 1, size, abfd) != size) return NULL; symbol_name = ptr; source_dll = ptr + strlen (ptr) + 1; /* Verify that the strings are null terminated. */ if (ptr[size - 1] != 0 || ((unsigned long) (source_dll - ptr) >= size)) { _bfd_error_handler (_("%s: string not null terminated in ILF object file."), bfd_get_filename (abfd)); bfd_set_error (bfd_error_malformed_archive); return NULL; } /* Now construct the bfd. */ if (! pe_ILF_build_a_bfd (abfd, magic, symbol_name, source_dll, ordinal, types)) return NULL; return abfd->xvec;}static const bfd_target *pe_bfd_object_p (bfd * abfd){ bfd_byte buffer[4]; struct external_PEI_DOS_hdr dos_hdr; struct external_PEI_IMAGE_hdr image_hdr; file_ptr offset; /* Detect if this a Microsoft Import Library Format element. */ if (bfd_seek (abfd, 0x00, SEEK_SET) != 0 || bfd_read (buffer, 1, 4, abfd) != 4) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return NULL; } if (bfd_h_get_32 (abfd, buffer) == 0xffff0000) return pe_ILF_object_p (abfd); if (bfd_seek (abfd, 0x00, SEEK_SET) != 0 || bfd_read (&dos_hdr, 1, sizeof (dos_hdr), abfd) != sizeof (dos_hdr)) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return NULL; } /* There are really two magic numbers involved; the magic number that says this is a NT executable (PEI) and the magic number that determines the architecture. The former is DOSMAGIC, stored in the e_magic field. The latter is stored in the f_magic field. If the NT magic number isn't valid, the architecture magic number could be mimicked by some other field (specifically, the number of relocs in section 3). Since this routine can only be called correctly for a PEI file, check the e_magic number here, and, if it doesn't match, clobber the f_magic number so that we don't get a false match. */ if (bfd_h_get_16 (abfd, (bfd_byte *) dos_hdr.e_magic) != DOSMAGIC) { bfd_set_error (bfd_error_wrong_format); return NULL; } offset = bfd_h_get_32 (abfd, (bfd_byte *) dos_hdr.e_lfanew); if (bfd_seek (abfd, (file_ptr) offset, SEEK_SET) != 0 || bfd_read (&image_hdr, 1, sizeof (image_hdr), abfd) != sizeof (image_hdr)) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return NULL; } if (bfd_h_get_32 (abfd, (bfd_byte *) image_hdr.nt_signature) != 0x4550) { bfd_set_error (bfd_error_wrong_format); return NULL; } /* Here is the hack. coff_object_p wants to read filhsz bytes to pick up the COFF header for PE, see "struct external_PEI_filehdr" in include/coff/pe.h. We adjust so that that will work. */ if (bfd_seek (abfd, (file_ptr) (offset - sizeof (dos_hdr)), SEEK_SET) != 0) { if (bfd_get_error () != bfd_error_system_call) bfd_set_error (bfd_error_wrong_format); return NULL; } return coff_object_p (abfd);}#define coff_object_p pe_bfd_object_p#endif /* COFF_IMAGE_WITH_PE */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?