On Fri, 2019-02-22 at 12:29 +0100, Richard Biener wrote:
> +struct build_id_note {
> +    /* The NHdr.  */
> +    uint32_t namesz;
> +    uint32_t descsz;
> +    uint32_t type;
> +
> +    char name[4]; /* Note name for build-id is "GNU\0" */
> +    unsigned char build_id[16];
> +};

Note that build-ids can be of different sizes depending on the style
used to generate them, you get the correct size by looking at the
descsz.

> +static int
> +get_build_id_1 (struct dl_phdr_info *info, size_t, void *data)
> +{
> +  for (unsigned i = 0; i < info->dlpi_phnum; ++i)
> +    {
> +      if (info->dlpi_phdr[i].p_type != PT_NOTE)
> +     continue;
> +      build_id_note *note
> +     = (build_id_note *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
> +      ptrdiff_t size = info->dlpi_phdr[i].p_filesz;
> +      while (size >= (ptrdiff_t)sizeof (build_id_note))
> +     {
> +       if (note->type == NT_GNU_BUILD_ID
> +           && note->namesz == 4
> +           && note->descsz >= 16)
> +         {
> +           memcpy (data, note->build_id, 16);
> +           return 1;
> +         }
> +       size_t offset = (sizeof (uint32_t) * 3
> +                        + ALIGN(note->namesz, 4)
> +                        + ALIGN(note->descsz, 4));
> +       note = (build_id_note *)((char *)note + offset);
> +       size -= offset;

Since the introduction of GNU Property notes this is (sadly) no longer
the correct way to iterate through ELF notes. The padding of names and
desc  might now depend on the alignment of the PT_NOTE segment.
https://sourceware.org/ml/binutils/2018-09/msg00359.html

Cheers,

Mark

Reply via email to