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; }
Dwfl_Error internal_function __libdwfl_relocate_section (Dwfl_Module *mod, Elf *relocated, Elf_Scn *relocscn, Elf_Scn *tscn, bool partial) { GElf_Ehdr ehdr_mem; GElf_Shdr shdr_mem; RELOC_SYMTAB_CACHE (reloc_symtab); size_t shstrndx; if (elf_getshdrstrndx (relocated, &shstrndx) < 0) return DWFL_E_LIBELF; return (__libdwfl_module_getebl (mod) ?: relocate_section (mod, relocated, gelf_getehdr (relocated, &ehdr_mem), shstrndx, &reloc_symtab, relocscn, gelf_getshdr (relocscn, &shdr_mem), tscn, false, partial)); }
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; }