Re: [PATCH] objtool: Fix symbol creation

Peter Zijlstra
2022-05-18T22:10:30Z

On Wed, May 18, 2022 at 10:36:04AM -0700, Josh Poimboeuf wrote:
> On Wed, May 18, 2022 at 09:41:52AM +0200, Peter Zijlstra wrote:
> > +static int elf_update_symbol(struct elf *elf, struct section *symtab,
> > + struct section *symtab_shndx, struct symbol *sym)
> > {
> > - Elf_Data *data, *shndx_data = NULL;
> > - Elf32_Word first_non_local;
> > - struct symbol *sym;
> > - Elf_Scn *s;
> > -
> > - first_non_local = symtab->sh.sh_info;
> > -
> > - sym = find_symbol_by_index(elf, first_non_local);
> > - if (!sym) {
> > - WARN("no non-local symbols !?");
> > - return first_non_local;
> > - }
> > + Elf_Data *symtab_data = NULL, *shndx_data = NULL;
> > + Elf64_Xword entsize = symtab->sh.sh_entsize;
> > + Elf32_Word shndx = sym->sec->idx;
>
> So if it's a global UNDEF symbol then I think 'sym->sec' can be NULL and
> this blows up?

Oh indeed, sym->sec ? sym->sec->idx : SHN_UNDEF it is.

> > + for (;;) {
> > + /* get next data descriptor for the relevant sections */
> > + symtab_data = elf_getdata(s, symtab_data);
> > + if (t)
> > + shndx_data = elf_getdata(t, shndx_data);
> > +
> > + /* end-of-list */
> > + if (!symtab_data) {
> > + /* if @idx == 0, it's the next contiguous entry, create it */
> > + if (!idx) {
> > + void *buf;
>
> Could just do the "index out of range warning" here to reduce the
> indentation level.

Sure.

> > + /* setup extended section index magic and write the symbol */
> > + if (shndx >= SHN_UNDEF && shndx < SHN_LORESERVE) {
>
> > + sym->sym.st_shndx = shndx;
> > + if (!shndx_data)
> > + shndx = 0;
>
> I think this '0' is SHN_UNDEF?
>
> Also shouldn't 'sym->sym.st_shndx' get the same value?

This is when there isn't an extended section index. Specifically
gelf_update_symshndx() requires that when @shndx_data == NULL, @shndx
must be 0 too.