static int mips_rela_to_rel (DSO *dso, GElf_Rela *rela, GElf_Rel *rel) { rel->r_offset = rela->r_offset; rel->r_info = rela->r_info; switch (GELF_R_TYPE (rela->r_info)) { case R_MIPS_NONE: break; case R_MIPS_REL32: case R_MIPS_TLS_DTPREL32: case R_MIPS_TLS_TPREL32: /* These relocations have an in-place addend. */ write_ne32 (dso, rela->r_offset, rela->r_addend); break; case R_MIPS_GLOB_DAT: case R_MIPS_TLS_DTPMOD32: /* These relocations have no addend. */ write_ne32 (dso, rela->r_offset, 0); break; default: error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename, (int) GELF_R_TYPE (rela->r_info)); return 1; } return 0; }
static void _dwarf_elf_apply_reloc(Dwarf_Debug dbg, void *buf, Elf_Data *rel_data, Elf_Data *symtab_data, int endian) { Dwarf_Unsigned type; GElf_Rela rela; GElf_Sym sym; size_t symndx; uint64_t offset; int size, j; j = 0; while (gelf_getrela(rel_data, j++, &rela) != NULL) { symndx = GELF_R_SYM(rela.r_info); type = GELF_R_TYPE(rela.r_info); if (gelf_getsym(symtab_data, symndx, &sym) == NULL) continue; offset = rela.r_offset; size = _dwarf_get_reloc_size(dbg, type); if (endian == ELFDATA2MSB) _dwarf_write_msb(buf, &offset, rela.r_addend, size); else _dwarf_write_lsb(buf, &offset, rela.r_addend, size); } }
static void _dwarf_elf_write_reloc(Dwarf_Debug dbg, Elf_Data *symtab_data, int endian, void *buf, uint64_t offset, GElf_Xword r_info, GElf_Sxword r_addend, int is_rel) { GElf_Sym sym; int size; if (gelf_getsym(symtab_data, GELF_R_SYM(r_info), &sym) == NULL) return; if ((size = _dwarf_get_reloc_size(dbg, GELF_R_TYPE(r_info))) == 0) return; /* Unknown or non-absolute relocation. */ if (is_rel) { uint64_t roffset = offset; if (endian == ELFDATA2MSB) r_addend = _dwarf_read_msb(buf, &roffset, size); else r_addend = _dwarf_read_lsb(buf, &roffset, size); } if (endian == ELFDATA2MSB) _dwarf_write_msb(buf, &offset, sym.st_value + r_addend, size); else _dwarf_write_lsb(buf, &offset, sym.st_value + r_addend, size); }
static int mips_prelink_rel (struct prelink_info *info, GElf_Rel *rel, GElf_Addr reladdr) { DSO *dso; /* Convert R_MIPS_REL32 relocations against global symbols into R_MIPS_GLOB_DAT if the addend is zero. */ dso = info->dso; if (GELF_R_TYPE (rel->r_info) == R_MIPS_REL32 && GELF_R_SYM (rel->r_info) >= dso->info_DT_MIPS_GOTSYM && read_une32 (dso, rel->r_offset) == 0) { rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_MIPS_GLOB_DAT); write_ne32 (dso, rel->r_offset, info->resolve (info, GELF_R_SYM (rel->r_info), GELF_R_TYPE (rel->r_info))); return 2; } return mips_prelink_reloc (info, rel->r_offset, rel->r_info, NULL); }
static int mips_apply_reloc (struct prelink_info *info, GElf_Xword r_info, GElf_Rela *rela, char *buf) { DSO *dso; dso = info->dso; switch (GELF_R_TYPE (r_info)) { case R_MIPS_NONE: break; case R_MIPS_REL32: mips_apply_adjustment (dso, rela, buf, info->resolve (info, GELF_R_SYM (r_info), GELF_R_TYPE (r_info))); break; default: return 1; } return 0; }
enum plt_status arch_elf_add_plt_entry(struct process *proc, struct ltelf *lte, const char *a_name, GElf_Rela *rela, size_t ndx, struct library_symbol **ret) { bool irelative = false; if (lte->ehdr.e_machine == EM_X86_64) { #ifdef R_X86_64_IRELATIVE irelative = GELF_R_TYPE(rela->r_info) == R_X86_64_IRELATIVE; #endif } else { assert(lte->ehdr.e_machine == EM_386); #ifdef R_386_IRELATIVE irelative = GELF_R_TYPE(rela->r_info) == R_386_IRELATIVE; #endif } if (irelative) return linux_elf_add_plt_entry_irelative(proc, lte, rela, ndx, ret); return PLT_DEFAULT; }
static int mips_apply_conflict_rela (struct prelink_info *info, GElf_Rela *rela, char *buf, GElf_Addr dest_addr) { switch (GELF_R_TYPE (rela->r_info)) { case R_MIPS_REL32: buf_write_ne32 (info->dso, buf, rela->r_addend); break; default: abort (); } return 0; }
static int mips_adjust_reloc (DSO *dso, GElf_Addr r_offset, GElf_Xword r_info, GElf_Addr start, GElf_Addr adjust, GElf_Rela *rela) { GElf_Addr value; GElf_Word r_sym; if (GELF_R_TYPE (r_info) == R_MIPS_REL32) { r_sym = GELF_R_SYM (r_info); if (r_sym < dso->info_DT_MIPS_GOTSYM) { /* glibc's dynamic linker adds the symbol's st_value and the base address to the addend. It therefore treats all symbols as being relative, even if they would normally be considered absolute. For example, the special null symbol should always have the value zero, even when the base address is nonzero, but R_MIPS_REL32 relocations against the null symbol must nevertheles be adjusted as if that symbol were relative. The same would apply to SHN_ABS symbols too. Thus the result of the relocation calculation must always be adjusted by ADJUST. (We do not need to check START because every adjustment requested by the caller will affect all legitimate local relocation values.) This means that we should add ADJUST to the addend if and only if the symbol's value is not being adjusted. In general, we can only check whether a symbol's value is being adjusted by reading its entry in the dynamic symbol table and then querying adjust_symbol_p. However, this generality is fortunately not needed. Modern versions of binutils will never generate R_MIPS_REL32 relocations against symbols in the range [1, DT_MIPS_GOTSYM), so we only need to handle relocations against the null symbol. */ if (r_sym != 0) { error (0, 0, "%s: The prelinker does not support R_MIPS_REL32" " relocs against local symbols", dso->filename); return 1; } value = mips_read_addend (dso, r_offset, rela); mips_write_addend (dso, r_offset, rela, value + adjust); } } return 0; }
static int mips_undo_prelink_rel (DSO *dso, GElf_Rel *rel, GElf_Addr reladdr) { /* Convert R_MIPS_GLOB_DAT relocations back into R_MIPS_REL32 relocations. Ideally we'd have some mechanism for recording these changes in the undo section, but in the absence of that, it's better to assume that the original relocation was R_MIPS_REL32; R_MIPS_GLOB_DAT was added specifically for the prelinker and shouldn't be used in non-prelinked binaries. */ if (GELF_R_TYPE (rel->r_info) == R_MIPS_GLOB_DAT) { write_ne32 (dso, rel->r_offset, 0); rel->r_info = GELF_R_INFO (GELF_R_SYM (rel->r_info), R_MIPS_REL32); return 2; } return 0; }
static int mips_need_rel_to_rela (DSO *dso, int first, int last) { Elf_Data *data; Elf_Scn *scn; Elf32_Rel *rel, *relend; int n; for (n = first; n <= last; n++) { data = NULL; scn = dso->scn[n]; while ((data = elf_getdata (scn, data)) != NULL) { rel = (Elf32_Rel *) data->d_buf; relend = rel + data->d_size / sizeof (Elf32_Rel); for (; rel < relend; rel++) switch (ELF32_R_TYPE (rel->r_info)) { case R_MIPS_NONE: break; case R_MIPS_REL32: /* The SVR4 definition was designed to allow exactly the sort of prelinking we want to do here, in combination with Quickstart. Unfortunately, glibc's definition makes it impossible for relocations against anything other than the null symbol. We get around this for zero addends by using a R_MIPS_GLOB_DAT relocation instead, where R_MIPS_GLOB_DAT is a GNU extension added specifically for this purpose. */ if (ELF32_R_SYM (rel->r_info) != 0 && (ELF32_R_SYM (rel->r_info) < dso->info_DT_MIPS_GOTSYM || read_une32 (dso, rel->r_offset) != 0)) return 1; break; case R_MIPS_GLOB_DAT: /* This relocation has no addend. */ break; case R_MIPS_TLS_DTPMOD32: /* The relocation will be resolved using a conflict. */ break; case R_MIPS_TLS_DTPREL32: /* We can prelink these fields, and the addend is relative to the symbol value. A RELA entry is needed. */ return 1; case R_MIPS_TLS_TPREL32: /* Relocations in shared libraries will be resolved by a conflict. Relocations in executables will not, and the addend is relative to the symbol value. */ if (dso->ehdr.e_type == ET_EXEC) return 1; break; default: error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename, (int) GELF_R_TYPE (rel->r_info)); return 1; } } } return 0; }
static int mips_prelink_conflict_reloc (DSO *dso, struct prelink_info *info, GElf_Addr r_offset, GElf_Xword r_info, GElf_Rela *rela) { GElf_Addr value; struct prelink_conflict *conflict; struct prelink_tls *tls = NULL; GElf_Rela *entry; if (info->dso == dso) return 0; conflict = prelink_conflict (info, GELF_R_SYM (r_info), GELF_R_TYPE (r_info)); if (conflict == NULL) { switch (GELF_R_TYPE (r_info)) { case R_MIPS_TLS_DTPMOD32: case R_MIPS_TLS_TPREL32: tls = info->curtls; if (tls == NULL) return 0; /* A relocation against symbol 0. A shared library cannot know what the final module IDs or TP-relative offsets are, so the executable must always have a conflict for them. */ value = 0; break; default: return 0; } } else if (conflict->ifunc) { error (0, 0, "%s: STT_GNU_IFUNC not handled on MIPS yet", dso->filename); return 1; } else { /* DTPREL32 relocations just involve the symbol value; no other TLS information is needed. Ignore conflicts created from a lookup of type RTYPE_CLASS_TLS if no real conflict exists. */ if (GELF_R_TYPE (r_info) == R_MIPS_TLS_DTPREL32 && conflict->lookup.tls == conflict->conflict.tls && conflict->lookupval == conflict->conflictval) return 0; value = conflict_lookup_value (conflict); } /* VALUE now contains the final symbol value. Change it to the value we want to store at R_OFFSET. */ switch (GELF_R_TYPE (r_info)) { case R_MIPS_REL32: value += mips_read_addend (dso, r_offset, rela); break; case R_MIPS_GLOB_DAT: break; case R_MIPS_TLS_DTPMOD32: if (conflict != NULL && mips_get_tls (dso, conflict, &tls) == 1) return 1; value = tls->modid; break; case R_MIPS_TLS_DTPREL32: value += mips_read_addend (dso, r_offset, rela) - TLS_DTV_OFFSET; break; case R_MIPS_TLS_TPREL32: if (conflict != NULL && mips_get_tls (dso, conflict, &tls) == 1) return 1; value += (mips_read_addend (dso, r_offset, rela) + tls->offset - TLS_TP_OFFSET); break; default: error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename, (int) GELF_R_TYPE (r_info)); return 1; } /* Create and initialize a conflict entry. */ entry = prelink_conflict_add_rela (info); if (entry == NULL) return 1; entry->r_addend = (int32_t) value; entry->r_offset = r_offset; entry->r_info = GELF_R_INFO (0, R_MIPS_REL32); return 0; }
static int mips_prelink_reloc (struct prelink_info *info, GElf_Addr r_offset, GElf_Xword r_info, GElf_Rela *rela) { DSO *dso; GElf_Addr value; GElf_Word r_sym; int r_type; dso = info->dso; r_sym = GELF_R_SYM (r_info); r_type = GELF_R_TYPE (r_info); switch (r_type) { case R_MIPS_NONE: break; case R_MIPS_REL32: /* An in-place R_MIPS_REL32 relocation against symbol 0 needs no adjustment. */ if (rela != NULL || GELF_R_SYM (r_info) != 0) { value = info->resolve (info, r_sym, r_type); mips_prelink_32bit_reloc (dso, rela, value); } break; case R_MIPS_GLOB_DAT: write_ne32 (dso, r_offset, info->resolve (info, r_sym, r_type)); break; case R_MIPS_TLS_DTPMOD32: if (dso->ehdr.e_type == ET_EXEC) { error (0, 0, "%s: R_MIPS_TLS_DTPMOD32 reloc in executable?", dso->filename); return 1; } /* These relocations will be resolved using a conflict. We need not change the field value here. */ break; case R_MIPS_TLS_DTPREL32: value = info->resolve (info, r_sym, r_type); mips_prelink_32bit_reloc (dso, rela, value - TLS_DTV_OFFSET); break; case R_MIPS_TLS_TPREL32: /* Relocations in a shared library will be resolved using a conflict. We need not change the relocation field here. */ if (dso->ehdr.e_type == ET_EXEC) { value = info->resolve (info, r_sym, r_type); value += info->resolvetls->offset - TLS_TP_OFFSET; mips_prelink_32bit_reloc (dso, rela, value); } break; default: error (0, 0, "%s: Unknown MIPS relocation type %d", dso->filename, (int) GELF_R_TYPE (r_info)); return 1; } return 0; }
static Dwfl_Error relocate_section (Dwfl_Module *mod, Elf *relocated, const GElf_Ehdr *ehdr, size_t shstrndx, struct reloc_symtab_cache *reloc_symtab, Elf_Scn *scn, GElf_Shdr *shdr, Elf_Scn *tscn, bool debugscn, bool partial) { /* First, fetch the name of the section these relocations apply to. */ GElf_Shdr tshdr_mem; GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem); const char *tname = elf_strptr (relocated, shstrndx, tshdr->sh_name); if (tname == NULL) return DWFL_E_LIBELF; if (unlikely (tshdr->sh_type == SHT_NOBITS) || unlikely (tshdr->sh_size == 0)) /* No contents to relocate. */ return DWFL_E_NOERROR; if (debugscn && ! ebl_debugscn_p (mod->ebl, tname)) /* This relocation section is not for a debugging section. Nothing to do here. */ return DWFL_E_NOERROR; /* Fetch the section data that needs the relocations applied. */ Elf_Data *tdata = elf_rawdata (tscn, NULL); if (tdata == NULL) return DWFL_E_LIBELF; /* Apply one relocation. Returns true for any invalid data. */ Dwfl_Error relocate (GElf_Addr offset, const GElf_Sxword *addend, int rtype, int symndx) { /* First see if this is a reloc we can handle. If we are skipping it, don't bother resolving the symbol. */ if (unlikely (rtype == 0)) /* In some odd situations, the linker can leave R_*_NONE relocs behind. This is probably bogus ld -r behavior, but the only cases it's known to appear in are harmless: DWARF data referring to addresses in a section that has been discarded. So we just pretend it's OK without further relocation. */ return DWFL_E_NOERROR; Elf_Type type = ebl_reloc_simple_type (mod->ebl, rtype); if (unlikely (type == ELF_T_NUM)) return DWFL_E_BADRELTYPE; /* First, resolve the symbol to an absolute value. */ GElf_Addr value; if (symndx == STN_UNDEF) /* When strip removes a section symbol referring to a section moved into the debuginfo file, it replaces that symbol index in relocs with STN_UNDEF. We don't actually need the symbol, because those relocs are always references relative to the nonallocated debugging sections, which start at zero. */ value = 0; else { GElf_Sym sym; GElf_Word shndx; Dwfl_Error error = relocate_getsym (mod, relocated, reloc_symtab, symndx, &sym, &shndx); if (unlikely (error != DWFL_E_NOERROR)) return error; if (shndx == SHN_UNDEF || shndx == SHN_COMMON) { /* Maybe we can figure it out anyway. */ error = resolve_symbol (mod, reloc_symtab, &sym, shndx); if (error != DWFL_E_NOERROR && !(error == DWFL_E_RELUNDEF && shndx == SHN_COMMON)) return error; } value = sym.st_value; } /* These are the types we can relocate. */ #define TYPES DO_TYPE (BYTE, Byte); DO_TYPE (HALF, Half); \ DO_TYPE (WORD, Word); DO_TYPE (SWORD, Sword); \ DO_TYPE (XWORD, Xword); DO_TYPE (SXWORD, Sxword) size_t size; switch (type) { #define DO_TYPE(NAME, Name) \ case ELF_T_##NAME: \ size = sizeof (GElf_##Name); \ break TYPES; #undef DO_TYPE default: return DWFL_E_BADRELTYPE; } if (offset + size > tdata->d_size) return DWFL_E_BADRELOFF; #define DO_TYPE(NAME, Name) GElf_##Name Name; union { TYPES; } tmpbuf; #undef DO_TYPE Elf_Data tmpdata = { .d_type = type, .d_buf = &tmpbuf, .d_size = size, .d_version = EV_CURRENT, }; Elf_Data rdata = { .d_type = type, .d_buf = tdata->d_buf + offset, .d_size = size, .d_version = EV_CURRENT, }; /* XXX check for overflow? */ if (addend) { /* For the addend form, we have the value already. */ value += *addend; switch (type) { #define DO_TYPE(NAME, Name) \ case ELF_T_##NAME: \ tmpbuf.Name = value; \ break TYPES; #undef DO_TYPE default: abort (); } } else { /* Extract the original value and apply the reloc. */ Elf_Data *d = gelf_xlatetom (relocated, &tmpdata, &rdata, ehdr->e_ident[EI_DATA]); if (d == NULL) return DWFL_E_LIBELF; assert (d == &tmpdata); switch (type) { #define DO_TYPE(NAME, Name) \ case ELF_T_##NAME: \ tmpbuf.Name += (GElf_##Name) value; \ break TYPES; #undef DO_TYPE default: abort (); } } /* Now convert the relocated datum back to the target format. This will write into rdata.d_buf, which points into the raw section data being relocated. */ Elf_Data *s = gelf_xlatetof (relocated, &rdata, &tmpdata, ehdr->e_ident[EI_DATA]); if (s == NULL) return DWFL_E_LIBELF; assert (s == &rdata); /* We have applied this relocation! */ return DWFL_E_NOERROR; } /* Fetch the relocation section and apply each reloc in it. */ Elf_Data *reldata = elf_getdata (scn, NULL); if (reldata == NULL) return DWFL_E_LIBELF; Dwfl_Error result = DWFL_E_NOERROR; bool first_badreltype = true; inline void check_badreltype (void) { if (first_badreltype) { first_badreltype = false; if (ebl_get_elfmachine (mod->ebl) == EM_NONE) /* This might be because ebl_openbackend failed to find any libebl_CPU.so library. Diagnose that clearly. */ result = DWFL_E_UNKNOWN_MACHINE; } } size_t nrels = shdr->sh_size / shdr->sh_entsize; size_t complete = 0; if (shdr->sh_type == SHT_REL) for (size_t relidx = 0; !result && relidx < nrels; ++relidx) { GElf_Rel rel_mem, *r = gelf_getrel (reldata, relidx, &rel_mem); if (r == NULL) return DWFL_E_LIBELF; result = relocate (r->r_offset, NULL, GELF_R_TYPE (r->r_info), GELF_R_SYM (r->r_info)); check_badreltype (); if (partial) switch (result) { case DWFL_E_NOERROR: /* We applied the relocation. Elide it. */ memset (&rel_mem, 0, sizeof rel_mem); gelf_update_rel (reldata, relidx, &rel_mem); ++complete; break; case DWFL_E_BADRELTYPE: case DWFL_E_RELUNDEF: /* We couldn't handle this relocation. Skip it. */ result = DWFL_E_NOERROR; break; default: break; } } else for (size_t relidx = 0; !result && relidx < nrels; ++relidx) { GElf_Rela rela_mem, *r = gelf_getrela (reldata, relidx, &rela_mem); if (r == NULL) return DWFL_E_LIBELF; result = relocate (r->r_offset, &r->r_addend, GELF_R_TYPE (r->r_info), GELF_R_SYM (r->r_info)); check_badreltype (); if (partial) switch (result) { case DWFL_E_NOERROR: /* We applied the relocation. Elide it. */ memset (&rela_mem, 0, sizeof rela_mem); gelf_update_rela (reldata, relidx, &rela_mem); ++complete; break; case DWFL_E_BADRELTYPE: case DWFL_E_RELUNDEF: /* We couldn't handle this relocation. Skip it. */ result = DWFL_E_NOERROR; break; default: break; } } if (likely (result == DWFL_E_NOERROR)) { if (!partial || complete == nrels) /* Mark this relocation section as being empty now that we have done its work. This affects unstrip -R, so e.g. it emits an empty .rela.debug_info along with a .debug_info that has already been fully relocated. */ nrels = 0; else if (complete != 0) { /* We handled some of the relocations but not all. We've zeroed out the ones we processed. Now remove them from the section. */ size_t next = 0; if (shdr->sh_type == SHT_REL) for (size_t relidx = 0; relidx < nrels; ++relidx) { GElf_Rel rel_mem; GElf_Rel *r = gelf_getrel (reldata, relidx, &rel_mem); if (r->r_info != 0 || r->r_offset != 0) { if (next != relidx) gelf_update_rel (reldata, next, r); ++next; } } else for (size_t relidx = 0; relidx < nrels; ++relidx) { GElf_Rela rela_mem; GElf_Rela *r = gelf_getrela (reldata, relidx, &rela_mem); if (r->r_info != 0 || r->r_offset != 0 || r->r_addend != 0) { if (next != relidx) gelf_update_rela (reldata, next, r); ++next; } } nrels = next; } shdr->sh_size = reldata->d_size = nrels * shdr->sh_entsize; gelf_update_shdr (scn, shdr); } return result; } Dwfl_Error internal_function __libdwfl_relocate (Dwfl_Module *mod, Elf *debugfile, bool debug) { assert (mod->e_type == ET_REL); GElf_Ehdr ehdr_mem; const GElf_Ehdr *ehdr = gelf_getehdr (debugfile, &ehdr_mem); if (ehdr == NULL) return DWFL_E_LIBELF; size_t d_shstrndx; if (elf_getshdrstrndx (debugfile, &d_shstrndx) < 0) return DWFL_E_LIBELF; RELOC_SYMTAB_CACHE (reloc_symtab); /* Look at each section in the debuginfo file, and process the relocation sections for debugging sections. */ Dwfl_Error result = DWFL_E_NOERROR; Elf_Scn *scn = NULL; while (result == DWFL_E_NOERROR && (scn = elf_nextscn (debugfile, scn)) != NULL) { GElf_Shdr shdr_mem; GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); if ((shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) && shdr->sh_size != 0) { /* It's a relocation section. */ Elf_Scn *tscn = elf_getscn (debugfile, shdr->sh_info); if (unlikely (tscn == NULL)) result = DWFL_E_LIBELF; else result = relocate_section (mod, debugfile, ehdr, d_shstrndx, &reloc_symtab, scn, shdr, tscn, debug, !debug); } } return result; }