xref: /aosp_15_r20/external/elfutils/libdwelf/dwelf_elf_gnu_debuglink.c (revision 7304104da70ce23c86437a01be71edd1a2d7f37e)
1*7304104dSAndroid Build Coastguard Worker /* Returns the file name and crc stored in the .gnu_debuglink if found.
2*7304104dSAndroid Build Coastguard Worker    Copyright (C) 2014 Red Hat, Inc.
3*7304104dSAndroid Build Coastguard Worker    This file is part of elfutils.
4*7304104dSAndroid Build Coastguard Worker 
5*7304104dSAndroid Build Coastguard Worker    This file is free software; you can redistribute it and/or modify
6*7304104dSAndroid Build Coastguard Worker    it under the terms of either
7*7304104dSAndroid Build Coastguard Worker 
8*7304104dSAndroid Build Coastguard Worker      * the GNU Lesser General Public License as published by the Free
9*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 3 of the License, or (at
10*7304104dSAndroid Build Coastguard Worker        your option) any later version
11*7304104dSAndroid Build Coastguard Worker 
12*7304104dSAndroid Build Coastguard Worker    or
13*7304104dSAndroid Build Coastguard Worker 
14*7304104dSAndroid Build Coastguard Worker      * the GNU General Public License as published by the Free
15*7304104dSAndroid Build Coastguard Worker        Software Foundation; either version 2 of the License, or (at
16*7304104dSAndroid Build Coastguard Worker        your option) any later version
17*7304104dSAndroid Build Coastguard Worker 
18*7304104dSAndroid Build Coastguard Worker    or both in parallel, as here.
19*7304104dSAndroid Build Coastguard Worker 
20*7304104dSAndroid Build Coastguard Worker    elfutils is distributed in the hope that it will be useful, but
21*7304104dSAndroid Build Coastguard Worker    WITHOUT ANY WARRANTY; without even the implied warranty of
22*7304104dSAndroid Build Coastguard Worker    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23*7304104dSAndroid Build Coastguard Worker    General Public License for more details.
24*7304104dSAndroid Build Coastguard Worker 
25*7304104dSAndroid Build Coastguard Worker    You should have received copies of the GNU General Public License and
26*7304104dSAndroid Build Coastguard Worker    the GNU Lesser General Public License along with this program.  If
27*7304104dSAndroid Build Coastguard Worker    not, see <http://www.gnu.org/licenses/>.  */
28*7304104dSAndroid Build Coastguard Worker 
29*7304104dSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
30*7304104dSAndroid Build Coastguard Worker # include <config.h>
31*7304104dSAndroid Build Coastguard Worker #endif
32*7304104dSAndroid Build Coastguard Worker 
33*7304104dSAndroid Build Coastguard Worker #include "libdwelfP.h"
34*7304104dSAndroid Build Coastguard Worker 
35*7304104dSAndroid Build Coastguard Worker const char *
dwelf_elf_gnu_debuglink(Elf * elf,GElf_Word * crc)36*7304104dSAndroid Build Coastguard Worker dwelf_elf_gnu_debuglink (Elf *elf, GElf_Word *crc)
37*7304104dSAndroid Build Coastguard Worker {
38*7304104dSAndroid Build Coastguard Worker   size_t shstrndx;
39*7304104dSAndroid Build Coastguard Worker   if (elf_getshdrstrndx (elf, &shstrndx) < 0)
40*7304104dSAndroid Build Coastguard Worker     return NULL;
41*7304104dSAndroid Build Coastguard Worker 
42*7304104dSAndroid Build Coastguard Worker   Elf_Scn *scn = NULL;
43*7304104dSAndroid Build Coastguard Worker   while ((scn = elf_nextscn (elf, scn)) != NULL)
44*7304104dSAndroid Build Coastguard Worker     {
45*7304104dSAndroid Build Coastguard Worker       GElf_Shdr shdr_mem;
46*7304104dSAndroid Build Coastguard Worker       GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem);
47*7304104dSAndroid Build Coastguard Worker       if (shdr == NULL)
48*7304104dSAndroid Build Coastguard Worker         return NULL;
49*7304104dSAndroid Build Coastguard Worker 
50*7304104dSAndroid Build Coastguard Worker       const char *name = elf_strptr (elf, shstrndx, shdr->sh_name);
51*7304104dSAndroid Build Coastguard Worker       if (name == NULL)
52*7304104dSAndroid Build Coastguard Worker         return NULL;
53*7304104dSAndroid Build Coastguard Worker 
54*7304104dSAndroid Build Coastguard Worker       if (!strcmp (name, ".gnu_debuglink"))
55*7304104dSAndroid Build Coastguard Worker         break;
56*7304104dSAndroid Build Coastguard Worker     }
57*7304104dSAndroid Build Coastguard Worker 
58*7304104dSAndroid Build Coastguard Worker   if (scn == NULL)
59*7304104dSAndroid Build Coastguard Worker     return NULL;
60*7304104dSAndroid Build Coastguard Worker 
61*7304104dSAndroid Build Coastguard Worker   /* Found the .gnu_debuglink section.  Extract its contents.  */
62*7304104dSAndroid Build Coastguard Worker   Elf_Data *rawdata = elf_rawdata (scn, NULL);
63*7304104dSAndroid Build Coastguard Worker   if (rawdata == NULL || rawdata->d_buf == NULL)
64*7304104dSAndroid Build Coastguard Worker     return NULL;
65*7304104dSAndroid Build Coastguard Worker 
66*7304104dSAndroid Build Coastguard Worker   /* The CRC comes after the zero-terminated file name,
67*7304104dSAndroid Build Coastguard Worker      (aligned up to 4 bytes) at the end of the section data.  */
68*7304104dSAndroid Build Coastguard Worker   if (rawdata->d_size <= sizeof *crc
69*7304104dSAndroid Build Coastguard Worker       || memchr (rawdata->d_buf, '\0', rawdata->d_size - sizeof *crc) == NULL)
70*7304104dSAndroid Build Coastguard Worker     return NULL;
71*7304104dSAndroid Build Coastguard Worker 
72*7304104dSAndroid Build Coastguard Worker   Elf_Data crcdata =
73*7304104dSAndroid Build Coastguard Worker     {
74*7304104dSAndroid Build Coastguard Worker       .d_type = ELF_T_WORD,
75*7304104dSAndroid Build Coastguard Worker       .d_buf = crc,
76*7304104dSAndroid Build Coastguard Worker       .d_size = sizeof *crc,
77*7304104dSAndroid Build Coastguard Worker       .d_version = EV_CURRENT,
78*7304104dSAndroid Build Coastguard Worker     };
79*7304104dSAndroid Build Coastguard Worker   Elf_Data conv =
80*7304104dSAndroid Build Coastguard Worker     {
81*7304104dSAndroid Build Coastguard Worker       .d_type = ELF_T_WORD,
82*7304104dSAndroid Build Coastguard Worker       .d_buf = rawdata->d_buf + rawdata->d_size - sizeof *crc,
83*7304104dSAndroid Build Coastguard Worker       .d_size = sizeof *crc,
84*7304104dSAndroid Build Coastguard Worker       .d_version = EV_CURRENT,
85*7304104dSAndroid Build Coastguard Worker     };
86*7304104dSAndroid Build Coastguard Worker 
87*7304104dSAndroid Build Coastguard Worker   GElf_Ehdr ehdr_mem;
88*7304104dSAndroid Build Coastguard Worker   GElf_Ehdr *ehdr = gelf_getehdr (elf, &ehdr_mem);
89*7304104dSAndroid Build Coastguard Worker   if (ehdr == NULL)
90*7304104dSAndroid Build Coastguard Worker     return NULL;
91*7304104dSAndroid Build Coastguard Worker 
92*7304104dSAndroid Build Coastguard Worker   Elf_Data *d = gelf_xlatetom (elf, &crcdata, &conv, ehdr->e_ident[EI_DATA]);
93*7304104dSAndroid Build Coastguard Worker   if (d == NULL)
94*7304104dSAndroid Build Coastguard Worker     return NULL;
95*7304104dSAndroid Build Coastguard Worker   assert (d == &crcdata);
96*7304104dSAndroid Build Coastguard Worker 
97*7304104dSAndroid Build Coastguard Worker   return rawdata->d_buf;
98*7304104dSAndroid Build Coastguard Worker }
99*7304104dSAndroid Build Coastguard Worker INTDEF(dwelf_elf_gnu_debuglink)
100