Re: [PATCH 5/9] objtool: Rework rebuild_reloc logic

From: Peter Zijlstra
Date: Fri Mar 19 2021 - 05:23:56 EST


On Thu, Mar 18, 2021 at 12:38:42PM -0500, Josh Poimboeuf wrote:
> On Thu, Mar 18, 2021 at 06:04:25PM +0100, Peter Zijlstra wrote:
> > On Thu, Mar 18, 2021 at 11:36:40AM -0500, Josh Poimboeuf wrote:
> > > > I was thinking you could get a section changed without touching
> > > > relocations, but while that is theoretically possible, it is exceedingly
> > > > unlikely (and objtool doesn't do that).
> > >
> > > Hm? This is a *relocation* section, not a normal one. So by
> > > definition, it only changes when its relocations change.
> >
> > The way I read this code:
> >
> > list_for_each_entry(sec, &elf->sections, list) {
> > if (sec->changed) {
> > + if (sec->reloc &&
> > + elf_rebuild_reloc_section(elf, sec->reloc)) {
> > + WARN_ELF("elf_rebuild_reloc_section");
> > + return -1;
> > + }
> >
> > is that we iterate the regular sections (which could be dirtied because
> > we changed some data), and if that section has a relocation section, we
> > rebuild that for good measure (even though it might not have altered
> > relocations).
> >
> > Or am I just totally confused ?
>
> Ah, you're right. I'm the one that's confused. I guess I was also
> confused when I wrote that hunk, but it just happens to work anyway.
>
> It would be cleaner to do something like
>
> if ((is_reloc_sec(sec) &&
> elf_rebuild_reloc_section(elf, sec)) {
>
> so we process the changed reloc section directly, instead of relying on
> the (most likely) fact that the corresponding text section also changed.

Indeed. Done.

--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -814,6 +814,11 @@ struct section *elf_create_reloc_section
}
}

+static inline bool is_reloc_section(struct section *reloc)
+{
+ return reloc->base && reloc->base->reloc == reloc;
+}
+
static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
{
struct reloc *reloc;
@@ -948,7 +953,7 @@ int elf_write(struct elf *elf)
/* Update changed relocation sections and section headers: */
list_for_each_entry(sec, &elf->sections, list) {
if (sec->changed) {
- if (sec->reloc &&
+ if (is_reloc_section(sec) &&
elf_rebuild_reloc_section(elf, sec->reloc)) {
WARN_ELF("elf_rebuild_reloc_section");
return -1;