13 #include <linux/zalloc.h>
14 #include <internal/lib.h>
16 static bool check_need_swap(int file_endian)
19 u8 *check = (u8 *)&data;
23 host_endian = ELFDATA2LSB;
25 host_endian = ELFDATA2MSB;
27 return host_endian != file_endian;
30 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
32 #define NT_GNU_BUILD_ID 3
34 static int read_build_id(void *note_data, size_t note_len, void *bf,
35 size_t size, bool need_swap)
45 while (ptr < (note_data + note_len)) {
47 size_t namesz, descsz;
51 nhdr->n_namesz = bswap_32(nhdr->n_namesz);
52 nhdr->n_descsz = bswap_32(nhdr->n_descsz);
53 nhdr->n_type = bswap_32(nhdr->n_type);
56 namesz = NOTE_ALIGN(nhdr->n_namesz);
57 descsz = NOTE_ALIGN(nhdr->n_descsz);
62 if (nhdr->n_type == NT_GNU_BUILD_ID &&
63 nhdr->n_namesz == sizeof("GNU")) {
64 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
65 size_t sz = min(size, descsz);
67 memset(bf + sz, 0, size - sz);
77 int filename__read_debuglink(const char *filename __maybe_unused,
78 char *debuglink __maybe_unused,
79 size_t size __maybe_unused)
85 * Just try PT_NOTE header otherwise fails
87 int filename__read_build_id(const char *filename, void *bf, size_t size)
91 bool need_swap = false;
92 u8 e_ident[EI_NIDENT];
97 fp = fopen(filename, "r");
101 if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
104 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
105 e_ident[EI_VERSION] != EV_CURRENT)
108 need_swap = check_need_swap(e_ident[EI_DATA]);
111 fseek(fp, 0, SEEK_SET);
113 if (e_ident[EI_CLASS] == ELFCLASS32) {
117 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
121 ehdr.e_phoff = bswap_32(ehdr.e_phoff);
122 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
123 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
126 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
127 buf = malloc(buf_size);
131 fseek(fp, ehdr.e_phoff, SEEK_SET);
132 if (fread(buf, buf_size, 1, fp) != 1)
135 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
140 phdr->p_type = bswap_32(phdr->p_type);
141 phdr->p_offset = bswap_32(phdr->p_offset);
142 phdr->p_filesz = bswap_32(phdr->p_filesz);
145 if (phdr->p_type != PT_NOTE)
148 buf_size = phdr->p_filesz;
149 offset = phdr->p_offset;
150 tmp = realloc(buf, buf_size);
155 fseek(fp, offset, SEEK_SET);
156 if (fread(buf, buf_size, 1, fp) != 1)
159 ret = read_build_id(buf, buf_size, bf, size, need_swap);
168 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
172 ehdr.e_phoff = bswap_64(ehdr.e_phoff);
173 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
174 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
177 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
178 buf = malloc(buf_size);
182 fseek(fp, ehdr.e_phoff, SEEK_SET);
183 if (fread(buf, buf_size, 1, fp) != 1)
186 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
191 phdr->p_type = bswap_32(phdr->p_type);
192 phdr->p_offset = bswap_64(phdr->p_offset);
193 phdr->p_filesz = bswap_64(phdr->p_filesz);
196 if (phdr->p_type != PT_NOTE)
199 buf_size = phdr->p_filesz;
200 offset = phdr->p_offset;
201 tmp = realloc(buf, buf_size);
206 fseek(fp, offset, SEEK_SET);
207 if (fread(buf, buf_size, 1, fp) != 1)
210 ret = read_build_id(buf, buf_size, bf, size, need_swap);
223 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
231 fd = open(filename, O_RDONLY);
235 if (fstat(fd, &stbuf) < 0)
238 buf_size = stbuf.st_size;
239 buf = malloc(buf_size);
243 if (read(fd, buf, buf_size) != (ssize_t) buf_size)
246 ret = read_build_id(buf, buf_size, build_id, size, false);
254 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
255 enum dso_binary_type type)
257 int fd = open(name, O_RDONLY);
261 ss->name = strdup(name);
272 dso->load_errno = errno;
276 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
278 /* Assume all sym sources could be a runtime image. */
282 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
287 void symsrc__destroy(struct symsrc *ss)
293 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
294 struct symsrc *ss __maybe_unused)
299 static int fd__is_64_bit(int fd)
301 u8 e_ident[EI_NIDENT];
303 if (lseek(fd, 0, SEEK_SET))
306 if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
309 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
310 e_ident[EI_VERSION] != EV_CURRENT)
313 return e_ident[EI_CLASS] == ELFCLASS64;
316 enum dso_type dso__type_fd(int fd)
321 ret = fd__is_64_bit(fd);
323 return DSO__TYPE_UNKNOWN;
326 return DSO__TYPE_64BIT;
328 if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
329 return DSO__TYPE_UNKNOWN;
331 if (ehdr.e_machine == EM_X86_64)
332 return DSO__TYPE_X32BIT;
334 return DSO__TYPE_32BIT;
337 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
339 struct symsrc *runtime_ss __maybe_unused,
340 int kmodule __maybe_unused)
342 unsigned char build_id[BUILD_ID_SIZE];
345 ret = fd__is_64_bit(ss->fd);
347 dso->is_64_bit = ret;
349 if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
350 dso__set_build_id(dso, build_id);
355 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
356 mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
357 bool *is_64_bit __maybe_unused)
362 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
367 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
371 int kcore_copy(const char *from_dir __maybe_unused,
372 const char *to_dir __maybe_unused)
377 void symbol__elf_init(void)
381 char *dso__demangle_sym(struct dso *dso __maybe_unused,
382 int kmodule __maybe_unused,
383 const char *elf_name __maybe_unused)