1 // SPDX-License-Identifier: GPL-2.0
14 static bool check_need_swap(int file_endian
)
17 u8
*check
= (u8
*)&data
;
21 host_endian
= ELFDATA2LSB
;
23 host_endian
= ELFDATA2MSB
;
25 return host_endian
!= file_endian
;
28 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
30 #define NT_GNU_BUILD_ID 3
32 static int read_build_id(void *note_data
, size_t note_len
, void *bf
,
33 size_t size
, bool need_swap
)
43 while (ptr
< (note_data
+ note_len
)) {
45 size_t namesz
, descsz
;
49 nhdr
->n_namesz
= bswap_32(nhdr
->n_namesz
);
50 nhdr
->n_descsz
= bswap_32(nhdr
->n_descsz
);
51 nhdr
->n_type
= bswap_32(nhdr
->n_type
);
54 namesz
= NOTE_ALIGN(nhdr
->n_namesz
);
55 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
60 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
61 nhdr
->n_namesz
== sizeof("GNU")) {
62 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
63 size_t sz
= min(size
, descsz
);
65 memset(bf
+ sz
, 0, size
- sz
);
75 int filename__read_debuglink(const char *filename __maybe_unused
,
76 char *debuglink __maybe_unused
,
77 size_t size __maybe_unused
)
83 * Just try PT_NOTE header otherwise fails
85 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
89 bool need_swap
= false;
90 u8 e_ident
[EI_NIDENT
];
95 fp
= fopen(filename
, "r");
99 if (fread(e_ident
, sizeof(e_ident
), 1, fp
) != 1)
102 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) ||
103 e_ident
[EI_VERSION
] != EV_CURRENT
)
106 need_swap
= check_need_swap(e_ident
[EI_DATA
]);
109 fseek(fp
, 0, SEEK_SET
);
111 if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
115 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1)
119 ehdr
.e_phoff
= bswap_32(ehdr
.e_phoff
);
120 ehdr
.e_phentsize
= bswap_16(ehdr
.e_phentsize
);
121 ehdr
.e_phnum
= bswap_16(ehdr
.e_phnum
);
124 buf_size
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
125 buf
= malloc(buf_size
);
129 fseek(fp
, ehdr
.e_phoff
, SEEK_SET
);
130 if (fread(buf
, buf_size
, 1, fp
) != 1)
133 for (i
= 0, phdr
= buf
; i
< ehdr
.e_phnum
; i
++, phdr
++) {
138 phdr
->p_type
= bswap_32(phdr
->p_type
);
139 phdr
->p_offset
= bswap_32(phdr
->p_offset
);
140 phdr
->p_filesz
= bswap_32(phdr
->p_filesz
);
143 if (phdr
->p_type
!= PT_NOTE
)
146 buf_size
= phdr
->p_filesz
;
147 offset
= phdr
->p_offset
;
148 tmp
= realloc(buf
, buf_size
);
153 fseek(fp
, offset
, SEEK_SET
);
154 if (fread(buf
, buf_size
, 1, fp
) != 1)
157 ret
= read_build_id(buf
, buf_size
, bf
, size
, need_swap
);
166 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1)
170 ehdr
.e_phoff
= bswap_64(ehdr
.e_phoff
);
171 ehdr
.e_phentsize
= bswap_16(ehdr
.e_phentsize
);
172 ehdr
.e_phnum
= bswap_16(ehdr
.e_phnum
);
175 buf_size
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
176 buf
= malloc(buf_size
);
180 fseek(fp
, ehdr
.e_phoff
, SEEK_SET
);
181 if (fread(buf
, buf_size
, 1, fp
) != 1)
184 for (i
= 0, phdr
= buf
; i
< ehdr
.e_phnum
; i
++, phdr
++) {
189 phdr
->p_type
= bswap_32(phdr
->p_type
);
190 phdr
->p_offset
= bswap_64(phdr
->p_offset
);
191 phdr
->p_filesz
= bswap_64(phdr
->p_filesz
);
194 if (phdr
->p_type
!= PT_NOTE
)
197 buf_size
= phdr
->p_filesz
;
198 offset
= phdr
->p_offset
;
199 tmp
= realloc(buf
, buf_size
);
204 fseek(fp
, offset
, SEEK_SET
);
205 if (fread(buf
, buf_size
, 1, fp
) != 1)
208 ret
= read_build_id(buf
, buf_size
, bf
, size
, need_swap
);
221 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
229 fd
= open(filename
, O_RDONLY
);
233 if (fstat(fd
, &stbuf
) < 0)
236 buf_size
= stbuf
.st_size
;
237 buf
= malloc(buf_size
);
241 if (read(fd
, buf
, buf_size
) != (ssize_t
) buf_size
)
244 ret
= read_build_id(buf
, buf_size
, build_id
, size
, false);
252 int symsrc__init(struct symsrc
*ss
, struct dso
*dso
, const char *name
,
253 enum dso_binary_type type
)
255 int fd
= open(name
, O_RDONLY
);
259 ss
->name
= strdup(name
);
270 dso
->load_errno
= errno
;
274 bool symsrc__possibly_runtime(struct symsrc
*ss __maybe_unused
)
276 /* Assume all sym sources could be a runtime image. */
280 bool symsrc__has_symtab(struct symsrc
*ss __maybe_unused
)
285 void symsrc__destroy(struct symsrc
*ss
)
291 int dso__synthesize_plt_symbols(struct dso
*dso __maybe_unused
,
292 struct symsrc
*ss __maybe_unused
)
297 static int fd__is_64_bit(int fd
)
299 u8 e_ident
[EI_NIDENT
];
301 if (lseek(fd
, 0, SEEK_SET
))
304 if (readn(fd
, e_ident
, sizeof(e_ident
)) != sizeof(e_ident
))
307 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) ||
308 e_ident
[EI_VERSION
] != EV_CURRENT
)
311 return e_ident
[EI_CLASS
] == ELFCLASS64
;
314 enum dso_type
dso__type_fd(int fd
)
319 ret
= fd__is_64_bit(fd
);
321 return DSO__TYPE_UNKNOWN
;
324 return DSO__TYPE_64BIT
;
326 if (readn(fd
, &ehdr
, sizeof(ehdr
)) != sizeof(ehdr
))
327 return DSO__TYPE_UNKNOWN
;
329 if (ehdr
.e_machine
== EM_X86_64
)
330 return DSO__TYPE_X32BIT
;
332 return DSO__TYPE_32BIT
;
335 int dso__load_sym(struct dso
*dso
, struct map
*map __maybe_unused
,
337 struct symsrc
*runtime_ss __maybe_unused
,
338 int kmodule __maybe_unused
)
340 unsigned char build_id
[BUILD_ID_SIZE
];
343 ret
= fd__is_64_bit(ss
->fd
);
345 dso
->is_64_bit
= ret
;
347 if (filename__read_build_id(ss
->name
, build_id
, BUILD_ID_SIZE
) > 0) {
348 dso__set_build_id(dso
, build_id
);
353 int file__read_maps(int fd __maybe_unused
, bool exe __maybe_unused
,
354 mapfn_t mapfn __maybe_unused
, void *data __maybe_unused
,
355 bool *is_64_bit __maybe_unused
)
360 int kcore_extract__create(struct kcore_extract
*kce __maybe_unused
)
365 void kcore_extract__delete(struct kcore_extract
*kce __maybe_unused
)
369 int kcore_copy(const char *from_dir __maybe_unused
,
370 const char *to_dir __maybe_unused
)
375 void symbol__elf_init(void)
379 char *dso__demangle_sym(struct dso
*dso __maybe_unused
,
380 int kmodule __maybe_unused
,
381 const char *elf_name __maybe_unused
)