Blame libdwelf/dwelf_elf_gnu_debuglink.c

Packit 032894
/* Returns the file name and crc stored in the .gnu_debuglink if found.
Packit 032894
   Copyright (C) 2014 Red Hat, Inc.
Packit 032894
   This file is part of elfutils.
Packit 032894
Packit 032894
   This file is free software; you can redistribute it and/or modify
Packit 032894
   it under the terms of either
Packit 032894
Packit 032894
     * the GNU Lesser General Public License as published by the Free
Packit 032894
       Software Foundation; either version 3 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or
Packit 032894
Packit 032894
     * the GNU General Public License as published by the Free
Packit 032894
       Software Foundation; either version 2 of the License, or (at
Packit 032894
       your option) any later version
Packit 032894
Packit 032894
   or both in parallel, as here.
Packit 032894
Packit 032894
   elfutils is distributed in the hope that it will be useful, but
Packit 032894
   WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 032894
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 032894
   General Public License for more details.
Packit 032894
Packit 032894
   You should have received copies of the GNU General Public License and
Packit 032894
   the GNU Lesser General Public License along with this program.  If
Packit 032894
   not, see <http://www.gnu.org/licenses/>.  */
Packit 032894
Packit 032894
#ifdef HAVE_CONFIG_H
Packit 032894
# include <config.h>
Packit 032894
#endif
Packit 032894
Packit 032894
#include "libdwelfP.h"
Packit 032894
Packit 032894
const char *
Packit 032894
dwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc)
Packit 032894
{
Packit 032894
  size_t shstrndx;
Packit 032894
  if (elf_getshdrstrndx (elf, &shstrndx) < 0)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  Elf_Scn *scn = NULL;
Packit 032894
  while ((scn = elf_nextscn (elf, scn)) != NULL)
Packit 032894
    {
Packit 032894
      GElf_Shdr shdr_mem;
Packit 032894
      GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
Packit 032894
      if (shdr == NULL)
Packit 032894
        return NULL;
Packit 032894
Packit 032894
      const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
Packit 032894
      if (name == NULL)
Packit 032894
        return NULL;
Packit 032894
Packit 032894
      if (!strcmp (name, ".gnu_debuglink"))
Packit 032894
        break;
Packit 032894
    }
Packit 032894
Packit 032894
  if (scn == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  /* Found the .gnu_debuglink section.  Extract its contents.  */
Packit 032894
  Elf_Data *rawdata = elf_rawdata (scn, NULL);
Packit 032894
  if (rawdata == NULL || rawdata->d_buf == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  /* The CRC comes after the zero-terminated file name,
Packit 032894
     (aligned up to 4 bytes) at the end of the section data.  */
Packit 032894
  if (rawdata->d_size <= sizeof *crc
Packit 032894
      || memchr (rawdata->d_buf, '\0', rawdata->d_size - sizeof *crc) == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  Elf_Data crcdata =
Packit 032894
    {
Packit 032894
      .d_type = ELF_T_WORD,
Packit 032894
      .d_buf = crc,
Packit 032894
      .d_size = sizeof *crc,
Packit 032894
      .d_version = EV_CURRENT,
Packit 032894
    };
Packit 032894
  Elf_Data conv =
Packit 032894
    {
Packit 032894
      .d_type = ELF_T_WORD,
Packit 032894
      .d_buf = rawdata->d_buf + rawdata->d_size - sizeof *crc,
Packit 032894
      .d_size = sizeof *crc,
Packit 032894
      .d_version = EV_CURRENT,
Packit 032894
    };
Packit 032894
Packit 032894
  GElf_Ehdr ehdr_mem;
Packit 032894
  GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
Packit 032894
  if (ehdr == NULL)
Packit 032894
    return NULL;
Packit 032894
Packit 032894
  Elf_Data *d = gelf_xlatetom (elf, &crcdata, &conv, ehdr->e_ident[EI_DATA]);
Packit 032894
  if (d == NULL)
Packit 032894
    return NULL;
Packit 032894
  assert (d == &crcdata);
Packit 032894
Packit 032894
  return rawdata->d_buf;
Packit 032894
}
Packit 032894
INTDEF(dwelf_elf_gnu_debuglink)