1 // SPDX-License-Identifier: GPL-2.0
15 static bool check_need_swap(int file_endian)
18 u8 *check = (u8 *)&data;
22 host_endian = ELFDATA2LSB;
24 host_endian = ELFDATA2MSB;
26 return host_endian != file_endian;
29 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
31 #define NT_GNU_BUILD_ID 3
33 static int read_build_id(void *note_data, size_t note_len, void *bf,
34 size_t size, bool need_swap)
44 while (ptr < (note_data + note_len)) {
46 size_t namesz, descsz;
50 nhdr->n_namesz = bswap_32(nhdr->n_namesz);
51 nhdr->n_descsz = bswap_32(nhdr->n_descsz);
52 nhdr->n_type = bswap_32(nhdr->n_type);
55 namesz = NOTE_ALIGN(nhdr->n_namesz);
56 descsz = NOTE_ALIGN(nhdr->n_descsz);
61 if (nhdr->n_type == NT_GNU_BUILD_ID &&
62 nhdr->n_namesz == sizeof("GNU")) {
63 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
64 size_t sz = min(size, descsz);
66 memset(bf + sz, 0, size - sz);
76 int filename__read_debuglink(const char *filename __maybe_unused,
77 char *debuglink __maybe_unused,
78 size_t size __maybe_unused)
84 * Just try PT_NOTE header otherwise fails
86 int filename__read_build_id(const char *filename, void *bf, size_t size)
90 bool need_swap = false;
91 u8 e_ident[EI_NIDENT];
96 fp = fopen(filename, "r");
100 if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
103 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
104 e_ident[EI_VERSION] != EV_CURRENT)
107 need_swap = check_need_swap(e_ident[EI_DATA]);
110 fseek(fp, 0, SEEK_SET);
112 if (e_ident[EI_CLASS] == ELFCLASS32) {
116 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
120 ehdr.e_phoff = bswap_32(ehdr.e_phoff);
121 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
122 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
125 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
126 buf = malloc(buf_size);
130 fseek(fp, ehdr.e_phoff, SEEK_SET);
131 if (fread(buf, buf_size, 1, fp) != 1)
134 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
139 phdr->p_type = bswap_32(phdr->p_type);
140 phdr->p_offset = bswap_32(phdr->p_offset);
141 phdr->p_filesz = bswap_32(phdr->p_filesz);
144 if (phdr->p_type != PT_NOTE)
147 buf_size = phdr->p_filesz;
148 offset = phdr->p_offset;
149 tmp = realloc(buf, buf_size);
154 fseek(fp, offset, SEEK_SET);
155 if (fread(buf, buf_size, 1, fp) != 1)
158 ret = read_build_id(buf, buf_size, bf, size, need_swap);
167 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
171 ehdr.e_phoff = bswap_64(ehdr.e_phoff);
172 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
173 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
176 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
177 buf = malloc(buf_size);
181 fseek(fp, ehdr.e_phoff, SEEK_SET);
182 if (fread(buf, buf_size, 1, fp) != 1)
185 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
190 phdr->p_type = bswap_32(phdr->p_type);
191 phdr->p_offset = bswap_64(phdr->p_offset);
192 phdr->p_filesz = bswap_64(phdr->p_filesz);
195 if (phdr->p_type != PT_NOTE)
198 buf_size = phdr->p_filesz;
199 offset = phdr->p_offset;
200 tmp = realloc(buf, buf_size);
205 fseek(fp, offset, SEEK_SET);
206 if (fread(buf, buf_size, 1, fp) != 1)
209 ret = read_build_id(buf, buf_size, bf, size, need_swap);
222 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
230 fd = open(filename, O_RDONLY);
234 if (fstat(fd, &stbuf) < 0)
237 buf_size = stbuf.st_size;
238 buf = malloc(buf_size);
242 if (read(fd, buf, buf_size) != (ssize_t) buf_size)
245 ret = read_build_id(buf, buf_size, build_id, size, false);
253 int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
254 enum dso_binary_type type)
256 int fd = open(name, O_RDONLY);
260 ss->name = strdup(name);
271 dso->load_errno = errno;
275 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
277 /* Assume all sym sources could be a runtime image. */
281 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
286 void symsrc__destroy(struct symsrc *ss)
292 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
293 struct symsrc *ss __maybe_unused)
298 static int fd__is_64_bit(int fd)
300 u8 e_ident[EI_NIDENT];
302 if (lseek(fd, 0, SEEK_SET))
305 if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
308 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
309 e_ident[EI_VERSION] != EV_CURRENT)
312 return e_ident[EI_CLASS] == ELFCLASS64;
315 enum dso_type dso__type_fd(int fd)
320 ret = fd__is_64_bit(fd);
322 return DSO__TYPE_UNKNOWN;
325 return DSO__TYPE_64BIT;
327 if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr))
328 return DSO__TYPE_UNKNOWN;
330 if (ehdr.e_machine == EM_X86_64)
331 return DSO__TYPE_X32BIT;
333 return DSO__TYPE_32BIT;
336 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
338 struct symsrc *runtime_ss __maybe_unused,
339 int kmodule __maybe_unused)
341 unsigned char build_id[BUILD_ID_SIZE];
344 ret = fd__is_64_bit(ss->fd);
346 dso->is_64_bit = ret;
348 if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
349 dso__set_build_id(dso, build_id);
354 int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused,
355 mapfn_t mapfn __maybe_unused, void *data __maybe_unused,
356 bool *is_64_bit __maybe_unused)
361 int kcore_extract__create(struct kcore_extract *kce __maybe_unused)
366 void kcore_extract__delete(struct kcore_extract *kce __maybe_unused)
370 int kcore_copy(const char *from_dir __maybe_unused,
371 const char *to_dir __maybe_unused)
376 void symbol__elf_init(void)
380 char *dso__demangle_sym(struct dso *dso __maybe_unused,
381 int kmodule __maybe_unused,
382 const char *elf_name __maybe_unused)