ecryptfs: convert to file_write_and_wait in ->fsync
[sfrench/cifs-2.6.git] / tools / perf / util / symbol-minimal.c
1 #include "symbol.h"
2 #include "util.h"
3
4 #include <errno.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <string.h>
8 #include <byteswap.h>
9 #include <sys/stat.h>
10
11
12 static bool check_need_swap(int file_endian)
13 {
14         const int data = 1;
15         u8 *check = (u8 *)&data;
16         int host_endian;
17
18         if (check[0] == 1)
19                 host_endian = ELFDATA2LSB;
20         else
21                 host_endian = ELFDATA2MSB;
22
23         return host_endian != file_endian;
24 }
25
26 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
27
28 #define NT_GNU_BUILD_ID 3
29
30 static int read_build_id(void *note_data, size_t note_len, void *bf,
31                          size_t size, bool need_swap)
32 {
33         struct {
34                 u32 n_namesz;
35                 u32 n_descsz;
36                 u32 n_type;
37         } *nhdr;
38         void *ptr;
39
40         ptr = note_data;
41         while (ptr < (note_data + note_len)) {
42                 const char *name;
43                 size_t namesz, descsz;
44
45                 nhdr = ptr;
46                 if (need_swap) {
47                         nhdr->n_namesz = bswap_32(nhdr->n_namesz);
48                         nhdr->n_descsz = bswap_32(nhdr->n_descsz);
49                         nhdr->n_type = bswap_32(nhdr->n_type);
50                 }
51
52                 namesz = NOTE_ALIGN(nhdr->n_namesz);
53                 descsz = NOTE_ALIGN(nhdr->n_descsz);
54
55                 ptr += sizeof(*nhdr);
56                 name = ptr;
57                 ptr += namesz;
58                 if (nhdr->n_type == NT_GNU_BUILD_ID &&
59                     nhdr->n_namesz == sizeof("GNU")) {
60                         if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
61                                 size_t sz = min(size, descsz);
62                                 memcpy(bf, ptr, sz);
63                                 memset(bf + sz, 0, size - sz);
64                                 return 0;
65                         }
66                 }
67                 ptr += descsz;
68         }
69
70         return -1;
71 }
72
73 int filename__read_debuglink(const char *filename __maybe_unused,
74                              char *debuglink __maybe_unused,
75                              size_t size __maybe_unused)
76 {
77         return -1;
78 }
79
80 /*
81  * Just try PT_NOTE header otherwise fails
82  */
83 int filename__read_build_id(const char *filename, void *bf, size_t size)
84 {
85         FILE *fp;
86         int ret = -1;
87         bool need_swap = false;
88         u8 e_ident[EI_NIDENT];
89         size_t buf_size;
90         void *buf;
91         int i;
92
93         fp = fopen(filename, "r");
94         if (fp == NULL)
95                 return -1;
96
97         if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
98                 goto out;
99
100         if (memcmp(e_ident, ELFMAG, SELFMAG) ||
101             e_ident[EI_VERSION] != EV_CURRENT)
102                 goto out;
103
104         need_swap = check_need_swap(e_ident[EI_DATA]);
105
106         /* for simplicity */
107         fseek(fp, 0, SEEK_SET);
108
109         if (e_ident[EI_CLASS] == ELFCLASS32) {
110                 Elf32_Ehdr ehdr;
111                 Elf32_Phdr *phdr;
112
113                 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
114                         goto out;
115
116                 if (need_swap) {
117                         ehdr.e_phoff = bswap_32(ehdr.e_phoff);
118                         ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
119                         ehdr.e_phnum = bswap_16(ehdr.e_phnum);
120                 }
121
122                 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
123                 buf = malloc(buf_size);
124                 if (buf == NULL)
125                         goto out;
126
127                 fseek(fp, ehdr.e_phoff, SEEK_SET);
128                 if (fread(buf, buf_size, 1, fp) != 1)
129                         goto out_free;
130
131                 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
132                         void *tmp;
133                         long offset;
134
135                         if (need_swap) {
136                                 phdr->p_type = bswap_32(phdr->p_type);
137                                 phdr->p_offset = bswap_32(phdr->p_offset);
138                                 phdr->p_filesz = bswap_32(phdr->p_filesz);
139                         }
140
141                         if (phdr->p_type != PT_NOTE)
142                                 continue;
143
144                         buf_size = phdr->p_filesz;
145                         offset = phdr->p_offset;
146                         tmp = realloc(buf, buf_size);
147                         if (tmp == NULL)
148                                 goto out_free;
149
150                         buf = tmp;
151                         fseek(fp, offset, SEEK_SET);
152                         if (fread(buf, buf_size, 1, fp) != 1)
153                                 goto out_free;
154
155                         ret = read_build_id(buf, buf_size, bf, size, need_swap);
156                         if (ret == 0)
157                                 ret = size;
158                         break;
159                 }
160         } else {
161                 Elf64_Ehdr ehdr;
162                 Elf64_Phdr *phdr;
163
164                 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
165                         goto out;
166
167                 if (need_swap) {
168                         ehdr.e_phoff = bswap_64(ehdr.e_phoff);
169                         ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
170                         ehdr.e_phnum = bswap_16(ehdr.e_phnum);
171                 }
172
173                 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
174                 buf = malloc(buf_size);
175                 if (buf == NULL)
176                         goto out;
177
178                 fseek(fp, ehdr.e_phoff, SEEK_SET);
179                 if (fread(buf, buf_size, 1, fp) != 1)
180                         goto out_free;
181
182                 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
183                         void *tmp;
184                         long offset;
185
186                         if (need_swap) {
187                                 phdr->p_type = bswap_32(phdr->p_type);
188                                 phdr->p_offset = bswap_64(phdr->p_offset);
189                                 phdr->p_filesz = bswap_64(phdr->p_filesz);
190                         }
191
192                         if (phdr->p_type != PT_NOTE)
193                                 continue;
194
195                         buf_size = phdr->p_filesz;
196                         offset = phdr->p_offset;
197                         tmp = realloc(buf, buf_size);
198                         if (tmp == NULL)
199                                 goto out_free;
200
201                         buf = tmp;
202                         fseek(fp, offset, SEEK_SET);
203                         if (fread(buf, buf_size, 1, fp) != 1)
204                                 goto out_free;
205
206                         ret = read_build_id(buf, buf_size, bf, size, need_swap);
207                         if (ret == 0)
208                                 ret = size;
209                         break;
210                 }
211         }
212 out_free:
213         free(buf);
214 out:
215         fclose(fp);
216         return ret;
217 }
218
219 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
220 {
221         int fd;
222         int ret = -1;
223         struct stat stbuf;
224         size_t buf_size;
225         void *buf;
226
227         fd = open(filename, O_RDONLY);
228         if (fd < 0)
229                 return -1;
230
231         if (fstat(fd, &stbuf) < 0)
232                 goto out;
233
234         buf_size = stbuf.st_size;
235         buf = malloc(buf_size);
236         if (buf == NULL)
237                 goto out;
238
239         if (read(fd, buf, buf_size) != (ssize_t) buf_size)
240                 goto out_free;
241
242         ret = read_build_id(buf, buf_size, build_id, size, false);
243 out_free:
244         free(buf);
245 out:
246         close(fd);
247         return ret;
248 }
249
250 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
251                  enum dso_binary_type type)
252 {
253         int fd = open(name, O_RDONLY);
254         if (fd < 0)
255                 goto out_errno;
256
257         ss->name = strdup(name);
258         if (!ss->name)
259                 goto out_close;
260
261         ss->fd = fd;
262         ss->type = type;
263
264         return 0;
265 out_close:
266         close(fd);
267 out_errno:
268         dso->load_errno = errno;
269         return -1;
270 }
271
272 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
273 {
274         /* Assume all sym sources could be a runtime image. */
275         return true;
276 }
277
278 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
279 {
280         return false;
281 }
282
283 void symsrc__destroy(struct symsrc *ss)
284 {
285         zfree(&ss->name);
286         close(ss->fd);
287 }
288
289 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
290                                 struct symsrc *ss __maybe_unused,
291                                 struct map *map __maybe_unused)
292 {
293         return 0;
294 }
295
296 static int fd__is_64_bit(int fd)
297 {
298         u8 e_ident[EI_NIDENT];
299
300         if (lseek(fd, 0, SEEK_SET))
301                 return -1;
302
303         if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
304                 return -1;
305
306         if (memcmp(e_ident, ELFMAG, SELFMAG) ||
307             e_ident[EI_VERSION] != EV_CURRENT)
308                 return -1;
309
310         return e_ident[EI_CLASS] == ELFCLASS64;
311 }
312
313 enum dso_type dso__type_fd(int fd)
314 {
315         Elf64_Ehdr ehdr;
316         int ret;
317
318         ret = fd__is_64_bit(fd);
319         if (ret < 0)
320                 return DSO__TYPE_UNKNOWN;
321
322         if (ret)
323                 return DSO__TYPE_64BIT;
324
325         if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
326                 return DSO__TYPE_UNKNOWN;
327
328         if (ehdr.e_machine == EM_X86_64)
329                 return DSO__TYPE_X32BIT;
330
331         return DSO__TYPE_32BIT;
332 }
333
334 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
335                   struct symsrc *ss,
336                   struct symsrc *runtime_ss __maybe_unused,
337                   int kmodule __maybe_unused)
338 {
339         unsigned char build_id[BUILD_ID_SIZE];
340         int ret;
341
342         ret = fd__is_64_bit(ss->fd);
343         if (ret >= 0)
344                 dso->is_64_bit = ret;
345
346         if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
347                 dso__set_build_id(dso, build_id);
348         }
349         return 0;
350 }
351
352 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
353                     mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
354                     bool *is_64_bit __maybe_unused)
355 {
356         return -1;
357 }
358
359 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
360 {
361         return -1;
362 }
363
364 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
365 {
366 }
367
368 int kcore_copy(const char *from_dir __maybe_unused,
369                const char *to_dir __maybe_unused)
370 {
371         return -1;
372 }
373
374 void symbol__elf_init(void)
375 {
376 }
377
378 char *dso__demangle_sym(struct dso *dso __maybe_unused,
379                         int kmodule __maybe_unused,
380                         char *elf_name __maybe_unused)
381 {
382         return NULL;
383 }