@@ -1294,6 +1297,135 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
return convert_reloc_secsym_to_sym(elf, reloc);
}
+static int correlate_reloc(struct elfs *e, struct symbol *func,
+ struct reloc *orig_reloc, struct reloc *patched_reloc)
+{
+ struct symbol *orig_sym, *patched_sym;
+ unsigned long offset = reloc_offset(orig_reloc) - func->offset;
+
+ if (offset != reloc_offset(patched_reloc) - func->twin->offset ||
+ reloc_type(orig_reloc) != reloc_type(patched_reloc)) {
+ ERROR("%s+%#lx: relocation mismatch in unchanged function", func->name, offset);
+ return -1;
+ }
+
+ if (reloc_type(orig_reloc) == R_NONE ||
+ is_uncorrelated_section(orig_reloc->sym->sec) ||
+ is_uncorrelated_section(patched_reloc->sym->sec))
+ return 0;
+
+ /* Section symbols have no twins; resolve the target symbol first. */
+ if (convert_reloc_secsym_to_sym(e->orig, orig_reloc) ||
+ convert_reloc_secsym_to_sym(e->patched, patched_reloc))
+ return 0;
+
+ orig_sym = orig_reloc->sym;
+ patched_sym = patched_reloc->sym;
+ if (orig_sym->dont_correlate || patched_sym->dont_correlate)
+ return 0;
+
+ if (reloc_addend(orig_reloc) != reloc_addend(patched_reloc)) {
+ ERROR("%s+%#lx: reference offset mismatch: %s[%u]%+lld and %s[%u]%+lld",
+ func->name, offset,
+ orig_sym->name, orig_sym->idx, (long long)reloc_addend(orig_reloc),
+ patched_sym->name, patched_sym->idx, (long long)reloc_addend(patched_reloc));
+ return -1;
+ }
+
+ if (orig_sym->twin == patched_sym && patched_sym->twin == orig_sym)
+ return 0;
+
+ if (orig_sym->twin || patched_sym->twin) {
+ ERROR("%s+%#lx: twin conflict: orig %s[%u]->%s[%u], patched %s[%u]->%s[%u]",
+ func->name, offset,
+ orig_sym->name, orig_sym->idx,
+ orig_sym->twin ? orig_sym->twin->name : "<none>",
+ orig_sym->twin ? orig_sym->twin->idx : 0,
+ patched_sym->name, patched_sym->idx,
+ patched_sym->twin ? patched_sym->twin->name : "<none>",
+ patched_sym->twin ? patched_sym->twin->idx : 0);
+ return -1;
+ }
+
+ /* Compound target checksums are independent of their defining TU. */
+ if (is_compound_constant(orig_sym) && is_compound_constant(patched_sym)) {
+ if (!orig_sym->csum.checksum || orig_sym->len != patched_sym->len ||
+ orig_sym->csum.checksum != patched_sym->csum.checksum) {
+ ERROR("%s+%#lx: compound content mismatch in unchanged function",
+ func->name, offset);
+ return -1;
+ }
+ } else if (!is_twin_candidate(orig_sym, patched_sym)) {
+ ERROR("%s+%#lx: incompatible reference targets: %s[%u] and %s[%u]",
+ func->name, offset, orig_sym->name, orig_sym->idx,
+ patched_sym->name, patched_sym->idx);
+ return -1;
+ }
+
+ orig_sym->twin = patched_sym;
+ patched_sym->twin = orig_sym;
+ dbg_correlate("%s(): %s+%#lx: %s[%u] -> %s[%u]",
+ __func__, func->name, offset,
+ orig_sym->name, orig_sym->idx, patched_sym->name, patched_sym->idx);
+ return 0;
+}
+
+static struct reloc *get_func_relocs(struct elf *elf, struct symbol *func, unsigned int *nr)
+{
+ struct reloc *first, *reloc;
+
+ first = find_reloc_by_dest_range(elf, func->sec, func->offset, func->len);
+ *nr = 0;
+ for (reloc = first; reloc && reloc_offset(reloc) < func->offset + func->len;
+ reloc = rsec_next_reloc(func->sec->rsec, reloc))
+ (*nr)++;
+ return first;
+}
+
+/* Use unchanged functions' references to resolve ambiguous symbol identities. */
+static int extend_twin_by_relocations(struct elfs *e, struct symbol *func)
+{
+ struct reloc *orig_relocs, *patched_relocs;
+ unsigned int nr_orig, nr_patched, i;
+
+ orig_relocs = get_func_relocs(e->orig, func, &nr_orig);
+ patched_relocs = get_func_relocs(e->patched, func->twin, &nr_patched);
+ if (nr_orig != nr_patched) {
+ ERROR("%s: relocation count mismatch in unchanged function: %u != %u",
+ func->name, nr_orig, nr_patched);
+ return -1;
+ }
+
+ for (i = 0; i < nr_orig; i++)
+ if (correlate_reloc(e, func, &orig_relocs[i], &patched_relocs[i]))
+ return -1;
+ return 0;
+}
+
+static bool same_function_checksum(struct symbol *func)
+{
+ /* Cold subfunctions have no separate checksum; use their parent's. */
+ struct symbol *parent = func->pfunc;
+ struct symbol *parent_twin = func->twin->pfunc;
+
+ return parent->twin == parent_twin && parent->csum.checksum &&
+ parent->csum.checksum == parent_twin->csum.checksum;
+}
+
+static int extend_twins_by_relocations(struct elfs *e)
+{
+ struct symbol *sym;
+
+ for_each_sym(e->orig, sym) {
+ if (!is_func_sym(sym) || sym->dont_correlate || !sym->twin ||
+ !same_function_checksum(sym))
+ continue;
+ if (extend_twin_by_relocations(e, sym))
+ return -1;
+ }
+ return 0;
+}
+
/*
* Check if the original module already has a dependency on dep_mod, i.e. it
* already references at least one export from that module.