11 static bool check_need_swap(int file_endian)
14 u8 *check = (u8 *)&data;
18 host_endian = ELFDATA2LSB;
20 host_endian = ELFDATA2MSB;
22 return host_endian != file_endian;
25 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
27 #define NT_GNU_BUILD_ID 3
29 static int read_build_id(void *note_data, size_t note_len, void *bf,
30 size_t size, bool need_swap)
40 while (ptr < (note_data + note_len)) {
42 size_t namesz, descsz;
46 nhdr->n_namesz = bswap_32(nhdr->n_namesz);
47 nhdr->n_descsz = bswap_32(nhdr->n_descsz);
48 nhdr->n_type = bswap_32(nhdr->n_type);
51 namesz = NOTE_ALIGN(nhdr->n_namesz);
52 descsz = NOTE_ALIGN(nhdr->n_descsz);
57 if (nhdr->n_type == NT_GNU_BUILD_ID &&
58 nhdr->n_namesz == sizeof("GNU")) {
59 if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
60 size_t sz = min(size, descsz);
62 memset(bf + sz, 0, size - sz);
72 int filename__read_debuglink(const char *filename __maybe_unused,
73 char *debuglink __maybe_unused,
74 size_t size __maybe_unused)
80 * Just try PT_NOTE header otherwise fails
82 int filename__read_build_id(const char *filename, void *bf, size_t size)
86 bool need_swap = false;
87 u8 e_ident[EI_NIDENT];
92 fp = fopen(filename, "r");
96 if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
99 if (memcmp(e_ident, ELFMAG, SELFMAG) ||
100 e_ident[EI_VERSION] != EV_CURRENT)
103 need_swap = check_need_swap(e_ident[EI_DATA]);
106 fseek(fp, 0, SEEK_SET);
108 if (e_ident[EI_CLASS] == ELFCLASS32) {
112 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
116 ehdr.e_phoff = bswap_32(ehdr.e_phoff);
117 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
118 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
121 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
122 buf = malloc(buf_size);
126 fseek(fp, ehdr.e_phoff, SEEK_SET);
127 if (fread(buf, buf_size, 1, fp) != 1)
130 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
134 phdr->p_type = bswap_32(phdr->p_type);
135 phdr->p_offset = bswap_32(phdr->p_offset);
136 phdr->p_filesz = bswap_32(phdr->p_filesz);
139 if (phdr->p_type != PT_NOTE)
142 buf_size = phdr->p_filesz;
143 tmp = realloc(buf, buf_size);
148 fseek(fp, phdr->p_offset, SEEK_SET);
149 if (fread(buf, buf_size, 1, fp) != 1)
152 ret = read_build_id(buf, buf_size, bf, size, need_swap);
161 if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
165 ehdr.e_phoff = bswap_64(ehdr.e_phoff);
166 ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
167 ehdr.e_phnum = bswap_16(ehdr.e_phnum);
170 buf_size = ehdr.e_phentsize * ehdr.e_phnum;
171 buf = malloc(buf_size);
175 fseek(fp, ehdr.e_phoff, SEEK_SET);
176 if (fread(buf, buf_size, 1, fp) != 1)
179 for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
183 phdr->p_type = bswap_32(phdr->p_type);
184 phdr->p_offset = bswap_64(phdr->p_offset);
185 phdr->p_filesz = bswap_64(phdr->p_filesz);
188 if (phdr->p_type != PT_NOTE)
191 buf_size = phdr->p_filesz;
192 tmp = realloc(buf, buf_size);
197 fseek(fp, phdr->p_offset, SEEK_SET);
198 if (fread(buf, buf_size, 1, fp) != 1)
201 ret = read_build_id(buf, buf_size, bf, size, need_swap);
214 int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
222 fd = open(filename, O_RDONLY);
226 if (fstat(fd, &stbuf) < 0)
229 buf_size = stbuf.st_size;
230 buf = malloc(buf_size);
234 if (read(fd, buf, buf_size) != (ssize_t) buf_size)
237 ret = read_build_id(buf, buf_size, build_id, size, false);
245 int symsrc__init(struct symsrc *ss, struct dso *dso __maybe_unused,
247 enum dso_binary_type type)
249 int fd = open(name, O_RDONLY);
253 ss->name = strdup(name);
265 bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused)
267 /* Assume all sym sources could be a runtime image. */
271 bool symsrc__has_symtab(struct symsrc *ss __maybe_unused)
276 void symsrc__destroy(struct symsrc *ss)
282 int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
283 struct symsrc *ss __maybe_unused,
284 struct map *map __maybe_unused,
285 symbol_filter_t filter __maybe_unused)
290 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
292 struct symsrc *runtime_ss __maybe_unused,
293 symbol_filter_t filter __maybe_unused,
294 int kmodule __maybe_unused)
296 unsigned char *build_id[BUILD_ID_SIZE];
298 if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
299 dso__set_build_id(dso, build_id);
305 void symbol__elf_init(void)