• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 
24 #ifdef HAVE_DL_ITERATE_PHDR
25 #include <dlfcn.h>
26 #include <link.h>
27 #include <stddef.h>
28 #include <string.h>
29 
30 #include "build_id.h"
31 
32 #ifndef NT_GNU_BUILD_ID
33 #define NT_GNU_BUILD_ID 3
34 #endif
35 
36 #ifndef ElfW
37 #define ElfW(type) Elf_##type
38 #endif
39 
40 #define ALIGN(val, align)      (((val) + (align) - 1) & ~((align) - 1))
41 
42 struct build_id_note {
43    ElfW(Nhdr) nhdr;
44 
45    char name[4]; /* Note name for build-id is "GNU\0" */
46    uint8_t build_id[0];
47 };
48 
49 struct callback_data {
50    /* Base address of shared object, taken from Dl_info::dli_fbase */
51    const void *dli_fbase;
52 
53    struct build_id_note *note;
54 };
55 
56 static int
build_id_find_nhdr_callback(struct dl_phdr_info * info,size_t size,void * data_)57 build_id_find_nhdr_callback(struct dl_phdr_info *info, size_t size, void *data_)
58 {
59    struct callback_data *data = data_;
60 
61    /* Calculate address where shared object is mapped into the process space.
62     * (Using the base address and the virtual address of the first LOAD segment)
63     */
64    void *map_start = NULL;
65    for (unsigned i = 0; i < info->dlpi_phnum; i++) {
66       if (info->dlpi_phdr[i].p_type == PT_LOAD) {
67          map_start = (void *)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
68          break;
69       }
70    }
71 
72    if (map_start != data->dli_fbase)
73       return 0;
74 
75    for (unsigned i = 0; i < info->dlpi_phnum; i++) {
76       if (info->dlpi_phdr[i].p_type != PT_NOTE)
77          continue;
78 
79       struct build_id_note *note = (void *)(info->dlpi_addr +
80                                             info->dlpi_phdr[i].p_vaddr);
81       ptrdiff_t len = info->dlpi_phdr[i].p_filesz;
82 
83       while (len >= sizeof(struct build_id_note)) {
84          if (note->nhdr.n_type == NT_GNU_BUILD_ID &&
85             note->nhdr.n_descsz != 0 &&
86             note->nhdr.n_namesz == 4 &&
87             memcmp(note->name, "GNU", 4) == 0) {
88             data->note = note;
89             return 1;
90          }
91 
92          size_t offset = sizeof(ElfW(Nhdr)) +
93                          ALIGN(note->nhdr.n_namesz, 4) +
94                          ALIGN(note->nhdr.n_descsz, 4);
95          note = (struct build_id_note *)((char *)note + offset);
96          len -= offset;
97       }
98    }
99 
100    return 0;
101 }
102 
103 const struct build_id_note *
build_id_find_nhdr_for_addr(const void * addr)104 build_id_find_nhdr_for_addr(const void *addr)
105 {
106    Dl_info info;
107 
108    if (!dladdr(addr, &info))
109       return NULL;
110 
111    if (!info.dli_fbase)
112       return NULL;
113 
114    struct callback_data data = {
115       .dli_fbase = info.dli_fbase,
116       .note = NULL,
117    };
118 
119    if (!dl_iterate_phdr(build_id_find_nhdr_callback, &data))
120       return NULL;
121 
122    return data.note;
123 }
124 
125 unsigned
build_id_length(const struct build_id_note * note)126 build_id_length(const struct build_id_note *note)
127 {
128    return note->nhdr.n_descsz;
129 }
130 
131 const uint8_t *
build_id_data(const struct build_id_note * note)132 build_id_data(const struct build_id_note *note)
133 {
134    return note->build_id;
135 }
136 
137 #endif
138