LoongArch: Don't write into GOT for local ifunc
authorXi Ruoyao <xry111@xry111.site>
Tue, 20 Sep 2022 06:09:29 +0000 (14:09 +0800)
committerliuzhensong <liuzhensong@loongson.cn>
Tue, 20 Sep 2022 09:16:05 +0000 (17:16 +0800)
Local ifuncs are always resolved at runtime via R_LARCH_IRELATIVE, so
there is no need to write anything into GOT.  And when we write the GOT
we actually trigger a heap-buffer-overflow: If a and b are different
sections, we cannot access something in b with "a->contents + (offset
from a)" because "a->contents" and "b->contents" are heap buffers
allocated separately, not slices of a large buffer.

So stop writing into GOT for local ifunc now.

bfd/elfnn-loongarch.c

index ed42b8b6770de27fe382ff9e0e7331f9b0c10bae..af18a8a01680cffe0535ec3fa967f7dae7526545 100644 (file)
@@ -3179,6 +3179,8 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
                                                     htab->elf.srelgot, &rela);
                        }
                      h->got.offset |= 1;
+                     bfd_put_NN (output_bfd, relocation,
+                                 got->contents + got_off);
                    }
                }
              else
@@ -3200,10 +3202,9 @@ loongarch_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
                        }
                      local_got_offsets[r_symndx] |= 1;
                    }
+                 bfd_put_NN (output_bfd, relocation, got->contents + got_off);
                }
 
-             bfd_put_NN (output_bfd, relocation, got->contents + got_off);
-
              relocation = got_off + sec_addr (got);
            }