elf32-i370.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 1,723 行 · 第 1/4 页
C
1,723 行
{ asection *target; const char *outname; /* Remember whether there are any relocation sections. */ relocs = true; /* If this relocation section applies to a read only section, then we probably need a DT_TEXTREL entry. */ outname = bfd_get_section_name (output_bfd, s->output_section); target = bfd_get_section_by_name (output_bfd, outname + 5); if (target != NULL && (target->flags & SEC_READONLY) != 0 && (target->flags & SEC_ALLOC) != 0) reltext = true; /* We use the reloc_count field as a counter if we need to copy relocs into the output file. */ s->reloc_count = 0; } } else if (strcmp (name, ".got") != 0 && strcmp (name, ".sdata") != 0 && strcmp (name, ".sdata2") != 0) { /* It's not one of our sections, so don't allocate space. */ continue; } if (strip) { asection **spp; for (spp = &s->output_section->owner->sections; *spp != s->output_section; spp = &(*spp)->next) ; *spp = s->output_section->next; --s->output_section->owner->section_count; continue; } /* Allocate memory for the section contents. */ s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->_raw_size); if (s->contents == NULL && s->_raw_size != 0) return false; } if (elf_hash_table (info)->dynamic_sections_created) { /* Add some entries to the .dynamic section. We fill in the values later, in i370_elf_finish_dynamic_sections, but we must add the entries now so that we get the correct size for the .dynamic section. The DT_DEBUG entry is filled in by the dynamic linker and used by the debugger. */ if (! info->shared) { if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)) return false; } if (plt) { if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0) || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0) || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_RELA) || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)) return false; } if (relocs) { if (! bfd_elf32_add_dynamic_entry (info, DT_RELA, 0) || ! bfd_elf32_add_dynamic_entry (info, DT_RELASZ, 0) || ! bfd_elf32_add_dynamic_entry (info, DT_RELAENT, sizeof (Elf32_External_Rela))) return false; } if (reltext) { if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0)) return false; info->flags |= DF_TEXTREL; } } /* If we are generating a shared library, we generate a section symbol for each output section. These are local symbols, which means that they must come first in the dynamic symbol table. That means we must increment the dynamic symbol index of every other dynamic symbol. FIXME: We assume that there will never be relocations to locations in linker-created sections that do not have externally-visible names. Instead, we should work out precisely which sections relocations are targetted at. */ if (info->shared) { int c; for (c = 0, s = output_bfd->sections; s != NULL; s = s->next) { if ((s->flags & SEC_LINKER_CREATED) != 0 || (s->flags & SEC_ALLOC) == 0) { elf_section_data (s)->dynindx = -1; continue; } /* These symbols will have no names, so we don't need to fiddle with dynstr_index. */ elf_section_data (s)->dynindx = c + 1; c++; } elf_link_hash_traverse (elf_hash_table (info), i370_elf_adjust_dynindx, (PTR) &c); elf_hash_table (info)->dynsymcount += c; } return true;}/* Look through the relocs for a section during the first phase, and allocate space in the global offset table or procedure linkage table. *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing. Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_elf_check_relocs (abfd, info, sec, relocs) bfd *abfd; struct bfd_link_info *info; asection *sec; const Elf_Internal_Rela *relocs;{ bfd *dynobj; Elf_Internal_Shdr *symtab_hdr; struct elf_link_hash_entry **sym_hashes; const Elf_Internal_Rela *rel; const Elf_Internal_Rela *rel_end; bfd_vma *local_got_offsets; asection *sreloc; if (info->relocateable) return true;#ifdef DEBUG fprintf (stderr, "i370_elf_check_relocs called for section %s in %s\n", bfd_get_section_name (abfd, sec), bfd_get_filename (abfd));#endif dynobj = elf_hash_table (info)->dynobj; symtab_hdr = &elf_tdata (abfd)->symtab_hdr; sym_hashes = elf_sym_hashes (abfd); local_got_offsets = elf_local_got_offsets (abfd); sreloc = NULL; rel_end = relocs + sec->reloc_count; for (rel = relocs; rel < rel_end; rel++) { unsigned long r_symndx; struct elf_link_hash_entry *h; r_symndx = ELF32_R_SYM (rel->r_info); if (r_symndx < symtab_hdr->sh_info) h = NULL; else h = sym_hashes[r_symndx - symtab_hdr->sh_info]; if (info->shared) {#ifdef DEBUG fprintf (stderr, "i370_elf_check_relocs needs to create relocation for %s\n", (h && h->root.root.string) ? h->root.root.string : "<unknown>");#endif if (sreloc == NULL) { const char *name; name = (bfd_elf_string_from_elf_section (abfd, elf_elfheader (abfd)->e_shstrndx, elf_section_data (sec)->rel_hdr.sh_name)); if (name == NULL) return false; BFD_ASSERT (strncmp (name, ".rela", 5) == 0 && strcmp (bfd_get_section_name (abfd, sec), name + 5) == 0); sreloc = bfd_get_section_by_name (dynobj, name); if (sreloc == NULL) { flagword flags; sreloc = bfd_make_section (dynobj, name); flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_IN_MEMORY | SEC_LINKER_CREATED); if ((sec->flags & SEC_ALLOC) != 0) flags |= SEC_ALLOC | SEC_LOAD; if (sreloc == NULL || ! bfd_set_section_flags (dynobj, sreloc, flags) || ! bfd_set_section_alignment (dynobj, sreloc, 2)) return false; } } sreloc->_raw_size += sizeof (Elf32_External_Rela); /* FIXME: We should here do what the m68k and i386 backends do: if the reloc is pc-relative, record it in case it turns out that the reloc is unnecessary because the symbol is forced local by versioning or we are linking with -Bdynamic. Fortunately this case is not frequent. */ } } return true;}/* Finish up the dynamic sections. *//* XXX hack alert bogus This routine is mostly all junk and almost * certainly does the wrong thing. Its here simply because it does * just enough to allow glibc-2.1 ld.so to compile & link. */static booleani370_elf_finish_dynamic_sections (output_bfd, info) bfd *output_bfd; struct bfd_link_info *info;{ asection *sdyn; bfd *dynobj = elf_hash_table (info)->dynobj; asection *sgot = bfd_get_section_by_name (dynobj, ".got");#ifdef DEBUG fprintf (stderr, "i370_elf_finish_dynamic_sections called\n");#endif sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); if (elf_hash_table (info)->dynamic_sections_created) { asection *splt; Elf32_External_Dyn *dyncon, *dynconend; splt = bfd_get_section_by_name (dynobj, ".plt"); BFD_ASSERT (splt != NULL && sdyn != NULL); dyncon = (Elf32_External_Dyn *) sdyn->contents; dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size); for (; dyncon < dynconend; dyncon++) { Elf_Internal_Dyn dyn; const char *name; boolean size; bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); switch (dyn.d_tag) { case DT_PLTGOT: name = ".plt"; size = false; break; case DT_PLTRELSZ: name = ".rela.plt"; size = true; break; case DT_JMPREL: name = ".rela.plt"; size = false; break; default: name = NULL; size = false; break; } if (name != NULL) { asection *s; s = bfd_get_section_by_name (output_bfd, name); if (s == NULL) dyn.d_un.d_val = 0; else { if (! size) dyn.d_un.d_ptr = s->vma; else { if (s->_cooked_size != 0) dyn.d_un.d_val = s->_cooked_size; else dyn.d_un.d_val = s->_raw_size; } } bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); } } } /* Add a blrl instruction at _GLOBAL_OFFSET_TABLE_-4 so that a function can easily find the address of the _GLOBAL_OFFSET_TABLE_. *//* XXX this is clearly very wrong for the 370 arch */ if (sgot) { unsigned char *contents = sgot->contents; bfd_put_32 (output_bfd, 0x4e800021 /* blrl */, contents); if (sdyn == NULL) bfd_put_32 (output_bfd, (bfd_vma) 0, contents+4); else bfd_put_32 (output_bfd, sdyn->output_section->vma + sdyn->output_offset, contents+4); elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; } if (info->shared) { asection *sdynsym; asection *s; Elf_Internal_Sym sym; int maxdindx = 0; /* Set up the section symbols for the output sections. */ sdynsym = bfd_get_section_by_name (dynobj, ".dynsym"); BFD_ASSERT (sdynsym != NULL); sym.st_size = 0; sym.st_name = 0; sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); sym.st_other = 0; for (s = output_bfd->sections; s != NULL; s = s->next) { int indx, dindx; sym.st_value = s->vma; indx = elf_section_data (s)->this_idx; dindx = elf_section_data (s)->dynindx; if (dindx != -1) { BFD_ASSERT(indx > 0); BFD_ASSERT(dindx > 0); if (dindx > maxdindx) maxdindx = dindx; sym.st_shndx = indx; bfd_elf32_swap_symbol_out (output_bfd, &sym, (PTR) (((Elf32_External_Sym *) sdynsym->contents) + dindx)); } } /* Set the sh_info field of the output .dynsym section to the index of the first global symbol. */ elf_section_data (sdynsym->output_section)->this_hdr.sh_info = maxdindx + 1; } return true;}/* The RELOCATE_SECTION function is called by the ELF backend linker to handle the relocations for a section. The relocs are always passed as Rela structures; if the section actually uses Rel structures, the r_addend field will always be zero. This function is responsible for adjust the section contents as necessary, and (if using Rela relocs and generating a relocateable output file) adjusting the reloc addend as necessary. This function does not have to worry about setting the reloc address or the reloc symbol index. LOCAL_SYMS is a pointer to the swapped in local symbols. LOCAL_SECTIONS is an array giving the section in the input file corresponding to the st_shndx field of each local symbol. The global hash table entry for the global symbols can be found via elf_sym_hashes (input_bfd). When generating relocateable output, this function must handle STB_LOCAL/STT_SECTION symbols specially. The output symbol is going to be the section symbol corresponding to the output section, which means that the addend must be adjusted accordingly. */static booleani370_elf_relocate_section (output_bfd, info, input_bfd, input_section, contents, relocs, local_syms, local_sections) bfd *output_bfd; struct bfd_link_info *info; bfd *input_bfd; asection *input_section; bfd_byte *contents; Elf_Internal_Rela *relocs; Elf_Internal_Sym *local_syms; asection **local_sections;{ Elf_Internal_Shdr *symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (input_bfd); bfd *dynobj = elf_hash_table (info)->dynobj; Elf_Internal_Rela *rel = relocs; Elf_Internal_Rela *relend = relocs + input_section->reloc_count; asection *sreloc = NULL; bfd_vma *local_got_offsets; boolean ret = true;#ifdef DEBUG fprintf (stderr, "i370_elf_relocate_section called for %s section %s, %ld relocations%s\n", bfd_get_filename (input_bfd), bfd_section_name(input_bfd, input_section), (long)input_section->reloc_count, (info->relocateable) ? " (relocatable)" : "");#endif if (!i370_elf_howto_table[ R_I370_ADDR31 ]) /* Initialize howto table if needed */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?