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, struct build_id *bid,
37 size_t size = sizeof(bid->data);
46 while (ptr < (note_data + note_len)) {
48 size_t namesz, descsz;
52 nhdr->n_namesz = bswap_32(nhdr->n_namesz);
53 nhdr->n_descsz = bswap_32(nhdr->n_descsz);
54 nhdr->n_type = bswap_32(nhdr->n_type);
57 namesz = NOTE_ALIGN(nhdr->n_namesz);
58 descsz = NOTE_ALIGN(nhdr->n_descsz);
63 if (nhdr->n_type == NT_GNU_BUILD_ID &&
64 nhdr->n_namesz == sizeof("GNU")) {
65 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
66 size_t sz = min(size, descsz);
67 memcpy(bid->data, ptr, sz);
68 memset(bid->data + sz, 0, size - sz);
79 int filename__read_debuglink(const char *filename __maybe_unused,
80 char *debuglink __maybe_unused,
81 size_t size __maybe_unused)
87 * Just try PT_NOTE header otherwise fails
89 int filename__read_build_id(const char *filename, struct build_id *bid)
93 bool need_swap = false;
94 u8 e_ident[EI_NIDENT];
99 fp = fopen(filename, "r");
103 if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
106 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
107 e_ident[EI_VERSION] != EV_CURRENT)
110 need_swap = check_need_swap(e_ident[EI_DATA]);
113 fseek(fp, 0, SEEK_SET);
115 if (e_ident[EI_CLASS] == ELFCLASS32) {
119 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
123 ehdr.e_phoff = bswap_32(ehdr.e_phoff);
124 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
125 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
128 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
129 buf = malloc(buf_size);
133 fseek(fp, ehdr.e_phoff, SEEK_SET);
134 if (fread(buf, buf_size, 1, fp) != 1)
137 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
142 phdr->p_type = bswap_32(phdr->p_type);
143 phdr->p_offset = bswap_32(phdr->p_offset);
144 phdr->p_filesz = bswap_32(phdr->p_filesz);
147 if (phdr->p_type != PT_NOTE)
150 buf_size = phdr->p_filesz;
151 offset = phdr->p_offset;
152 tmp = realloc(buf, buf_size);
157 fseek(fp, offset, SEEK_SET);
158 if (fread(buf, buf_size, 1, fp) != 1)
161 ret = read_build_id(buf, buf_size, bid, need_swap);
170 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
174 ehdr.e_phoff = bswap_64(ehdr.e_phoff);
175 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
176 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
179 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
180 buf = malloc(buf_size);
184 fseek(fp, ehdr.e_phoff, SEEK_SET);
185 if (fread(buf, buf_size, 1, fp) != 1)
188 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
193 phdr->p_type = bswap_32(phdr->p_type);
194 phdr->p_offset = bswap_64(phdr->p_offset);
195 phdr->p_filesz = bswap_64(phdr->p_filesz);
198 if (phdr->p_type != PT_NOTE)
201 buf_size = phdr->p_filesz;
202 offset = phdr->p_offset;
203 tmp = realloc(buf, buf_size);
208 fseek(fp, offset, SEEK_SET);
209 if (fread(buf, buf_size, 1, fp) != 1)
212 ret = read_build_id(buf, buf_size, bid, need_swap);
225 int sysfs__read_build_id(const char *filename, struct build_id *bid)
233 fd = open(filename, O_RDONLY);
237 if (fstat(fd, &stbuf) < 0)
240 buf_size = stbuf.st_size;
241 buf = malloc(buf_size);
245 if (read(fd, buf, buf_size) != (ssize_t) buf_size)
248 ret = read_build_id(buf, buf_size, bid, false);
256 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
257 enum dso_binary_type type)
259 int fd = open(name, O_RDONLY);
263 ss->name = strdup(name);
274 dso->load_errno = errno;
278 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
280 /* Assume all sym sources could be a runtime image. */
284 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
289 void symsrc__destroy(struct symsrc *ss)
295 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
296 struct symsrc *ss __maybe_unused)
301 static int fd__is_64_bit(int fd)
303 u8 e_ident[EI_NIDENT];
305 if (lseek(fd, 0, SEEK_SET))
308 if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
311 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
312 e_ident[EI_VERSION] != EV_CURRENT)
315 return e_ident[EI_CLASS] == ELFCLASS64;
318 enum dso_type dso__type_fd(int fd)
323 ret = fd__is_64_bit(fd);
325 return DSO__TYPE_UNKNOWN;
328 return DSO__TYPE_64BIT;
330 if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
331 return DSO__TYPE_UNKNOWN;
333 if (ehdr.e_machine == EM_X86_64)
334 return DSO__TYPE_X32BIT;
336 return DSO__TYPE_32BIT;
339 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
341 struct symsrc *runtime_ss __maybe_unused,
342 int kmodule __maybe_unused)
347 ret = fd__is_64_bit(ss->fd);
349 dso->is_64_bit = ret;
351 if (filename__read_build_id(ss->name, &bid) > 0)
352 dso__set_build_id(dso, &bid);
356 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
357 mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
358 bool *is_64_bit __maybe_unused)
363 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
368 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
372 int kcore_copy(const char *from_dir __maybe_unused,
373 const char *to_dir __maybe_unused)
378 void symbol__elf_init(void)
382 char *dso__demangle_sym(struct dso *dso __maybe_unused,
383 int kmodule __maybe_unused,
384 const char *elf_name __maybe_unused)