📄 elf32-i386.c
字号:
{ case R_386_GOT32: case R_386_GOTOFF: case R_386_GOTPC: r_symndx = ELF32_R_SYM (rel->r_info); if (r_symndx >= symtab_hdr->sh_info) { h = sym_hashes[r_symndx - symtab_hdr->sh_info]; if (h->got.refcount > 0) { h->got.refcount -= 1; if (h->got.refcount == 0) { sgot->_raw_size -= 4; srelgot->_raw_size -= sizeof (Elf32_External_Rel); } } } else if (local_got_refcounts != NULL) { if (local_got_refcounts[r_symndx] > 0) { local_got_refcounts[r_symndx] -= 1; if (local_got_refcounts[r_symndx] == 0) { sgot->_raw_size -= 4; if (info->shared) srelgot->_raw_size -= sizeof (Elf32_External_Rel); } } } break; case R_386_PLT32: r_symndx = ELF32_R_SYM (rel->r_info); if (r_symndx >= symtab_hdr->sh_info) { h = sym_hashes[r_symndx - symtab_hdr->sh_info]; if (h->plt.refcount > 0) h->plt.refcount -= 1; } break; default: break; } return true;}/* Adjust a symbol defined by a dynamic object and referenced by a regular object. The current definition is in some section of the dynamic object, but we're not including those sections. We have to change the definition to something the rest of the link can understand. */static booleanelf_i386_adjust_dynamic_symbol (info, h) struct bfd_link_info *info; struct elf_link_hash_entry *h;{ bfd *dynobj; asection *s; unsigned int power_of_two; dynobj = elf_hash_table (info)->dynobj; /* Make sure we know what is going on here. */ BFD_ASSERT (dynobj != NULL && ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) || h->weakdef != NULL || ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0))); /* If this is a function, put it in the procedure linkage table. We will fill in the contents of the procedure linkage table later, when we know the address of the .got section. */ if (h->type == STT_FUNC || (h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_PLT) != 0) { if ((! info->shared && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_DYNAMIC) == 0) || (info->shared && h->plt.refcount <= 0)) { /* This case can occur if we saw a PLT32 reloc in an input file, but the symbol was never referred to by a dynamic object, or if all references were garbage collected. In such a case, we don't actually need to build a procedure linkage table, and we can just do a PC32 reloc instead. */ h->plt.offset = (bfd_vma) -1; h->elf_link_hash_flags &= ~ELF_LINK_HASH_NEEDS_PLT; return true; } /* Make sure this symbol is output as a dynamic symbol. */ if (h->dynindx == -1) { if (! bfd_elf32_link_record_dynamic_symbol (info, h)) return false; } s = bfd_get_section_by_name (dynobj, ".plt"); BFD_ASSERT (s != NULL); /* If this is the first .plt entry, make room for the special first entry. */ if (s->_raw_size == 0) s->_raw_size += PLT_ENTRY_SIZE; /* If this symbol is not defined in a regular file, and we are not generating a shared library, then set the symbol to this location in the .plt. This is required to make function pointers compare as equal between the normal executable and the shared library. */ if (! info->shared && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) { h->root.u.def.section = s; h->root.u.def.value = s->_raw_size; } h->plt.offset = s->_raw_size; /* Make room for this entry. */ s->_raw_size += PLT_ENTRY_SIZE; /* We also need to make an entry in the .got.plt section, which will be placed in the .got section by the linker script. */ s = bfd_get_section_by_name (dynobj, ".got.plt"); BFD_ASSERT (s != NULL); s->_raw_size += 4; /* We also need to make an entry in the .rel.plt section. */ s = bfd_get_section_by_name (dynobj, ".rel.plt"); BFD_ASSERT (s != NULL); s->_raw_size += sizeof (Elf32_External_Rel); return true; } /* If this is a weak symbol, and there is a real definition, the processor independent code will have arranged for us to see the real definition first, and we can just use the same value. */ if (h->weakdef != NULL) { BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined || h->weakdef->root.type == bfd_link_hash_defweak); h->root.u.def.section = h->weakdef->root.u.def.section; h->root.u.def.value = h->weakdef->root.u.def.value; return true; } /* This is a reference to a symbol defined by a dynamic object which is not a function. */ /* If we are creating a shared library, we must presume that the only references to the symbol are via the global offset table. For such cases we need not do anything here; the relocations will be handled correctly by relocate_section. */ if (info->shared) return true; /* If there are no references to this symbol that do not use the GOT, we don't need to generate a copy reloc. */ if ((h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0) return true; /* We must allocate the symbol in our .dynbss section, which will become part of the .bss section of the executable. There will be an entry for this symbol in the .dynsym section. The dynamic object will contain position independent code, so all references from the dynamic object to this symbol will go through the global offset table. The dynamic linker will use the .dynsym entry to determine the address it must put in the global offset table, so both the dynamic object and the regular object will refer to the same memory location for the variable. */ s = bfd_get_section_by_name (dynobj, ".dynbss"); BFD_ASSERT (s != NULL); /* We must generate a R_386_COPY reloc to tell the dynamic linker to copy the initial value out of the dynamic object and into the runtime process image. We need to remember the offset into the .rel.bss section we are going to use. */ if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) { asection *srel; srel = bfd_get_section_by_name (dynobj, ".rel.bss"); BFD_ASSERT (srel != NULL); srel->_raw_size += sizeof (Elf32_External_Rel); h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; } /* We need to figure out the alignment required for this symbol. I have no idea how ELF linkers handle this. */ power_of_two = bfd_log2 (h->size); if (power_of_two > 3) power_of_two = 3; /* Apply the required alignment. */ s->_raw_size = BFD_ALIGN (s->_raw_size, (bfd_size_type) (1 << power_of_two)); if (power_of_two > bfd_get_section_alignment (dynobj, s)) { if (! bfd_set_section_alignment (dynobj, s, power_of_two)) return false; } /* Define the symbol as being at this point in the section. */ h->root.u.def.section = s; h->root.u.def.value = s->_raw_size; /* Increment the section size to make room for the symbol. */ s->_raw_size += h->size; return true;}/* Set the sizes of the dynamic sections. */static booleanelf_i386_size_dynamic_sections (output_bfd, info) bfd *output_bfd; struct bfd_link_info *info;{ bfd *dynobj; asection *s; boolean plt; boolean relocs; boolean reltext; dynobj = elf_hash_table (info)->dynobj; BFD_ASSERT (dynobj != NULL); if (elf_hash_table (info)->dynamic_sections_created) { /* Set the contents of the .interp section to the interpreter. */ if (! info->shared) { s = bfd_get_section_by_name (dynobj, ".interp"); BFD_ASSERT (s != NULL); s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; } } else { /* We may have created entries in the .rel.got section. However, if we are not creating the dynamic sections, we will not actually use these entries. Reset the size of .rel.got, which will cause it to get stripped from the output file below. */ s = bfd_get_section_by_name (dynobj, ".rel.got"); if (s != NULL) s->_raw_size = 0; } /* If this is a -Bsymbolic shared link, then we need to discard all PC relative relocs against symbols defined in a regular object. We allocated space for them in the check_relocs routine, but we will not fill them in in the relocate_section routine. */ if (info->shared) elf_i386_link_hash_traverse (elf_i386_hash_table (info), elf_i386_discard_copies, (PTR) info); /* The check_relocs and adjust_dynamic_symbol entry points have determined the sizes of the various dynamic sections. Allocate memory for them. */ plt = false; relocs = false; reltext = false; for (s = dynobj->sections; s != NULL; s = s->next) { const char *name; boolean strip; if ((s->flags & SEC_LINKER_CREATED) == 0) continue; /* It's OK to base decisions on the section name, because none of the dynobj section names depend upon the input files. */ name = bfd_get_section_name (dynobj, s); strip = false; if (strcmp (name, ".plt") == 0) { if (s->_raw_size == 0) { /* Strip this section if we don't need it; see the comment below. */ strip = true; } else { /* Remember whether there is a PLT. */ plt = true; } } else if (strncmp (name, ".rel", 4) == 0) { if (s->_raw_size == 0) { /* If we don't need this section, strip it from the output file. This is mostly to handle .rel.bss and .rel.plt. We must create both sections in create_dynamic_sections, because they must be created before the linker maps input sections to output sections. The linker does that before adjust_dynamic_symbol is called, and it is that function which decides whether anything needs to go into these sections. */ strip = true; } else { asection *target; /* Remember whether there are any reloc sections other than .rel.plt. */ if (strcmp (name, ".rel.plt") != 0) { const char *outname; relocs = true; /* If this relocation section applies to a read only section, then we probably need a DT_TEXTREL entry. The entries in the .rel.plt section really apply to the .got section, which we created ourselves and so know is not readonly. */ outname = bfd_get_section_name (output_bfd, s->output_section); target = bfd_get_section_by_name (output_bfd, outname + 4); 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 (strncmp (name, ".got", 4) != 0) { /* It's not one of our sections, so don't allocate space. */ continue; } if (strip) { _bfd_strip_section_from_output (info, s); continue; } /* Allocate memory for the section contents. We use bfd_zalloc here in case unused entries are not reclaimed before the section's contents are written out. This should not happen, but this way if it does, we get a R_386_NONE reloc instead of garbage. */ 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 elf_i386_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_REL) || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)) return false; } if (relocs) { if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0) || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0) || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT, sizeof (Elf32_External_Rel))) return false; } if (reltext) { if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0)) return false; info->flags |= DF_TEXTREL; } } return true;}/* This function is called via elf_i386_link_hash_traverse if we are creating a shared object. In the -Bsymbolic case, it discards the space allocated to copy PC relative relocs against symbols which are defined in regular objects. For the normal non-symbolic case, we also discard space for relocs that have become local due to symbol visibility changes. We allocated space for them in the check_relocs routine, but we won't fill them in in the relocate_section routine. */static booleanelf_i386_discard_copies (h, inf) struct elf_i386_link_hash_entry *h; PTR inf;{ struct elf_i386_pcrel_relocs_copied *s;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -