packaging: update homepage url
[platform/upstream/elfutils.git] / libdwfl / dwfl_module_build_id.c
1 /* Return build ID information for a module.
2    Copyright (C) 2007-2010, 2014 Red Hat, Inc.
3    This file is part of elfutils.
4
5    This file is free software; you can redistribute it and/or modify
6    it under the terms of either
7
8      * the GNU Lesser General Public License as published by the Free
9        Software Foundation; either version 3 of the License, or (at
10        your option) any later version
11
12    or
13
14      * the GNU General Public License as published by the Free
15        Software Foundation; either version 2 of the License, or (at
16        your option) any later version
17
18    or both in parallel, as here.
19
20    elfutils is distributed in the hope that it will be useful, but
21    WITHOUT ANY WARRANTY; without even the implied warranty of
22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23    General Public License for more details.
24
25    You should have received copies of the GNU General Public License and
26    the GNU Lesser General Public License along with this program.  If
27    not, see <http://www.gnu.org/licenses/>.  */
28
29 #include "libdwflP.h"
30
31 static int
32 found_build_id (Dwfl_Module *mod, bool set,
33                 const void *bits, int len, GElf_Addr vaddr)
34 {
35   if (!set)
36     /* When checking bits, we do not compare VADDR because the
37        address found in a debuginfo file may not match the main
38        file as modified by prelink.  */
39     return 1 + (mod->build_id_len == len
40                 && !memcmp (bits, mod->build_id_bits, len));
41
42   void *copy = malloc (len);
43   if (unlikely (copy == NULL))
44     {
45       __libdwfl_seterrno (DWFL_E_NOMEM);
46       return -1;
47     }
48
49   mod->build_id_bits = memcpy (copy, bits, len);
50   mod->build_id_vaddr = vaddr;
51   mod->build_id_len = len;
52   return len;
53 }
54
55 int
56 internal_function
57 __libdwfl_find_build_id (Dwfl_Module *mod, bool set, Elf *elf)
58 {
59   const void *build_id_bits;
60   GElf_Addr build_id_elfaddr;
61   int build_id_len;
62
63   /* For mod == NULL use dwelf_elf_gnu_build_id directly.  */
64   assert (mod != NULL);
65
66   int result = __libdwfl_find_elf_build_id (mod, elf, &build_id_bits,
67                                             &build_id_elfaddr, &build_id_len);
68   if (result <= 0)
69     return result;
70
71   GElf_Addr build_id_vaddr = build_id_elfaddr + (build_id_elfaddr != 0
72                                                  ? mod->main_bias : 0);
73   return found_build_id (mod, set, build_id_bits, build_id_len, build_id_vaddr);
74 }
75
76 int
77 dwfl_module_build_id (Dwfl_Module *mod,
78                       const unsigned char **bits, GElf_Addr *vaddr)
79 {
80   if (mod == NULL)
81     return -1;
82
83   if (mod->build_id_len == 0 && mod->main.elf != NULL)
84     {
85       /* We have the file, but have not examined it yet.  */
86       int result = __libdwfl_find_build_id (mod, true, mod->main.elf);
87       if (result <= 0)
88         {
89           mod->build_id_len = -1;       /* Cache negative result.  */
90           return result;
91         }
92     }
93
94   if (mod->build_id_len <= 0)
95     return 0;
96
97   *bits = mod->build_id_bits;
98   *vaddr = mod->build_id_vaddr;
99   return mod->build_id_len;
100 }
101 INTDEF (dwfl_module_build_id)
102 NEW_VERSION (dwfl_module_build_id, ELFUTILS_0.138)
103
104 #ifdef SYMBOL_VERSIONING
105 COMPAT_VERSION (dwfl_module_build_id, ELFUTILS_0.130, vaddr_at_end)
106
107 int
108 _compat_vaddr_at_end_dwfl_module_build_id (Dwfl_Module *mod,
109                                            const unsigned char **bits,
110                                            GElf_Addr *vaddr)
111 {
112   int result = INTUSE(dwfl_module_build_id) (mod, bits, vaddr);
113   if (result > 0)
114     *vaddr += (result + 3) & -4;
115   return result;
116 }
117 #endif