103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes/* Returns the file name and crc stored in the .gnu_debuglink if found. 203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Copyright (C) 2014 Red Hat, Inc. 303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes This file is part of elfutils. 403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes This file is free software; you can redistribute it and/or modify 603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes it under the terms of either 703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes * the GNU Lesser General Public License as published by the Free 903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Software Foundation; either version 3 of the License, or (at 1003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes your option) any later version 1103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes or 1303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes * the GNU General Public License as published by the Free 1503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Software Foundation; either version 2 of the License, or (at 1603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes your option) any later version 1703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 1803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes or both in parallel, as here. 1903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes elfutils is distributed in the hope that it will be useful, but 2103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes WITHOUT ANY WARRANTY; without even the implied warranty of 2203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 2303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes General Public License for more details. 2403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes You should have received copies of the GNU General Public License and 2603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes the GNU Lesser General Public License along with this program. If 2703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes not, see <http://www.gnu.org/licenses/>. */ 2803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 2903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#ifdef HAVE_CONFIG_H 3003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes# include <config.h> 3103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#endif 3203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes#include "libdwelfP.h" 3403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 3503333823c75a1c1887e923828113a1b0fd12020cElliott Hughesconst char * 3603333823c75a1c1887e923828113a1b0fd12020cElliott Hughesdwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc) 3703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes{ 3803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes size_t shstrndx; 3903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (elf_getshdrstrndx (elf, &shstrndx) < 0) 4003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 4103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 4203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Scn *scn = NULL; 4303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes while ((scn = elf_nextscn (elf, scn)) != NULL) 4403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 4503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes GElf_Shdr shdr_mem; 4603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); 4703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (shdr == NULL) 4803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 4903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes const char *name = elf_strptr (elf, shstrndx, shdr->sh_name); 5103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (name == NULL) 5203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 5303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (!strcmp (name, ".gnu_debuglink")) 5503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes break; 5603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes } 5703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 5803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (scn == NULL) 5903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 6003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 6103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* Found the .gnu_debuglink section. Extract its contents. */ 6203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Data *rawdata = elf_rawdata (scn, NULL); 6303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (rawdata == NULL || rawdata->d_buf == NULL) 6403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 6503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 6603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes /* The CRC comes after the zero-terminated file name, 6703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes (aligned up to 4 bytes) at the end of the section data. */ 6803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (rawdata->d_size <= sizeof *crc 6903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes || memchr (rawdata->d_buf, '\0', rawdata->d_size - sizeof *crc) == NULL) 7003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 7103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 7203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Data crcdata = 7303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 7403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_type = ELF_T_WORD, 7503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_buf = crc, 7603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_size = sizeof *crc, 7703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_version = EV_CURRENT, 7803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes }; 7903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Data conv = 8003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes { 8103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_type = ELF_T_WORD, 8203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_buf = rawdata->d_buf + rawdata->d_size - sizeof *crc, 8303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_size = sizeof *crc, 8403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes .d_version = EV_CURRENT, 8503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes }; 8603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 8703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes GElf_Ehdr ehdr_mem; 8803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem); 8903333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (ehdr == NULL) 9003333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 9103333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 9203333823c75a1c1887e923828113a1b0fd12020cElliott Hughes Elf_Data *d = gelf_xlatetom (elf, &crcdata, &conv, ehdr->e_ident[EI_DATA]); 9303333823c75a1c1887e923828113a1b0fd12020cElliott Hughes if (d == NULL) 9403333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return NULL; 9503333823c75a1c1887e923828113a1b0fd12020cElliott Hughes assert (d == &crcdata); 9603333823c75a1c1887e923828113a1b0fd12020cElliott Hughes 9703333823c75a1c1887e923828113a1b0fd12020cElliott Hughes return rawdata->d_buf; 9803333823c75a1c1887e923828113a1b0fd12020cElliott Hughes} 9903333823c75a1c1887e923828113a1b0fd12020cElliott HughesINTDEF(dwelf_elf_gnu_debuglink) 100