util/sha1: rework _mesa_sha1_{init,final}
[mesa.git] / src / util / build_id.c
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 <link.h>
26 #include <stddef.h>
27 #include <string.h>
28
29 #include "build_id.h"
30
31 #ifndef NT_GNU_BUILD_ID
32 #define NT_GNU_BUILD_ID 3
33 #endif
34
35 #ifndef ElfW
36 #define ElfW(type) Elf_##type
37 #endif
38
39 #define ALIGN(val, align) (((val) + (align) - 1) & ~((align) - 1))
40
41 struct build_id_note {
42 ElfW(Nhdr) nhdr;
43
44 char name[4]; /* Note name for build-id is "GNU\0" */
45 uint8_t build_id[0];
46 };
47
48 struct callback_data {
49 const char *filename;
50 struct build_id_note *note;
51 };
52
53 static int
54 build_id_find_nhdr_callback(struct dl_phdr_info *info, size_t size, void *data_)
55 {
56 struct callback_data *data = data_;
57
58 char *ptr = strstr(info->dlpi_name, data->filename);
59 if (ptr == NULL || ptr[strlen(data->filename)] != '\0')
60 return 0;
61
62 for (unsigned i = 0; i < info->dlpi_phnum; i++) {
63 if (info->dlpi_phdr[i].p_type != PT_NOTE)
64 continue;
65
66 struct build_id_note *note = (void *)(info->dlpi_addr +
67 info->dlpi_phdr[i].p_vaddr);
68 ptrdiff_t len = info->dlpi_phdr[i].p_filesz;
69
70 while (len >= sizeof(struct build_id_note)) {
71 if (note->nhdr.n_type == NT_GNU_BUILD_ID &&
72 note->nhdr.n_descsz != 0 &&
73 note->nhdr.n_namesz == 4 &&
74 memcmp(note->name, "GNU", 4) == 0) {
75 data->note = note;
76 return 1;
77 }
78
79 size_t offset = sizeof(ElfW(Nhdr)) +
80 ALIGN(note->nhdr.n_namesz, 4) +
81 ALIGN(note->nhdr.n_descsz, 4);
82 note = (struct build_id_note *)((char *)note + offset);
83 len -= offset;
84 }
85 }
86
87 return 0;
88 }
89
90 const struct build_id_note *
91 build_id_find_nhdr(const char *filename)
92 {
93 struct callback_data data = {
94 .filename = filename,
95 .note = NULL,
96 };
97
98 if (!dl_iterate_phdr(build_id_find_nhdr_callback, &data))
99 return NULL;
100
101 return data.note;
102 }
103
104 unsigned
105 build_id_length(const struct build_id_note *note)
106 {
107 return note->nhdr.n_descsz;
108 }
109
110 const uint8_t *
111 build_id_data(const struct build_id_note *note)
112 {
113 return note->build_id;
114 }
115
116 #endif