10 static bool check_need_swap(int file_endian
)
13 u8
*check
= (u8
*)&data
;
17 host_endian
= ELFDATA2LSB
;
19 host_endian
= ELFDATA2MSB
;
21 return host_endian
!= file_endian
;
24 #define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
26 #define NT_GNU_BUILD_ID 3
28 static int read_build_id(void *note_data
, size_t note_len
, void *bf
,
29 size_t size
, bool need_swap
)
39 while (ptr
< (note_data
+ note_len
)) {
41 size_t namesz
, descsz
;
45 nhdr
->n_namesz
= bswap_32(nhdr
->n_namesz
);
46 nhdr
->n_descsz
= bswap_32(nhdr
->n_descsz
);
47 nhdr
->n_type
= bswap_32(nhdr
->n_type
);
50 namesz
= NOTE_ALIGN(nhdr
->n_namesz
);
51 descsz
= NOTE_ALIGN(nhdr
->n_descsz
);
56 if (nhdr
->n_type
== NT_GNU_BUILD_ID
&&
57 nhdr
->n_namesz
== sizeof("GNU")) {
58 if (memcmp(name
, "GNU", sizeof("GNU")) == 0) {
59 size_t sz
= min(size
, descsz
);
61 memset(bf
+ sz
, 0, size
- sz
);
71 int filename__read_debuglink(const char *filename __maybe_unused
,
72 char *debuglink __maybe_unused
,
73 size_t size __maybe_unused
)
79 * Just try PT_NOTE header otherwise fails
81 int filename__read_build_id(const char *filename
, void *bf
, size_t size
)
85 bool need_swap
= false;
86 u8 e_ident
[EI_NIDENT
];
91 fp
= fopen(filename
, "r");
95 if (fread(e_ident
, sizeof(e_ident
), 1, fp
) != 1)
98 if (memcmp(e_ident
, ELFMAG
, SELFMAG
) ||
99 e_ident
[EI_VERSION
] != EV_CURRENT
)
102 need_swap
= check_need_swap(e_ident
[EI_DATA
]);
105 fseek(fp
, 0, SEEK_SET
);
107 if (e_ident
[EI_CLASS
] == ELFCLASS32
) {
111 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1)
115 ehdr
.e_phoff
= bswap_32(ehdr
.e_phoff
);
116 ehdr
.e_phentsize
= bswap_16(ehdr
.e_phentsize
);
117 ehdr
.e_phnum
= bswap_16(ehdr
.e_phnum
);
120 buf_size
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
121 buf
= malloc(buf_size
);
125 fseek(fp
, ehdr
.e_phoff
, SEEK_SET
);
126 if (fread(buf
, buf_size
, 1, fp
) != 1)
129 for (i
= 0, phdr
= buf
; i
< ehdr
.e_phnum
; i
++, phdr
++) {
133 phdr
->p_type
= bswap_32(phdr
->p_type
);
134 phdr
->p_offset
= bswap_32(phdr
->p_offset
);
135 phdr
->p_filesz
= bswap_32(phdr
->p_filesz
);
138 if (phdr
->p_type
!= PT_NOTE
)
141 buf_size
= phdr
->p_filesz
;
142 tmp
= realloc(buf
, buf_size
);
147 fseek(fp
, phdr
->p_offset
, SEEK_SET
);
148 if (fread(buf
, buf_size
, 1, fp
) != 1)
151 ret
= read_build_id(buf
, buf_size
, bf
, size
, need_swap
);
160 if (fread(&ehdr
, sizeof(ehdr
), 1, fp
) != 1)
164 ehdr
.e_phoff
= bswap_64(ehdr
.e_phoff
);
165 ehdr
.e_phentsize
= bswap_16(ehdr
.e_phentsize
);
166 ehdr
.e_phnum
= bswap_16(ehdr
.e_phnum
);
169 buf_size
= ehdr
.e_phentsize
* ehdr
.e_phnum
;
170 buf
= malloc(buf_size
);
174 fseek(fp
, ehdr
.e_phoff
, SEEK_SET
);
175 if (fread(buf
, buf_size
, 1, fp
) != 1)
178 for (i
= 0, phdr
= buf
; i
< ehdr
.e_phnum
; i
++, phdr
++) {
182 phdr
->p_type
= bswap_32(phdr
->p_type
);
183 phdr
->p_offset
= bswap_64(phdr
->p_offset
);
184 phdr
->p_filesz
= bswap_64(phdr
->p_filesz
);
187 if (phdr
->p_type
!= PT_NOTE
)
190 buf_size
= phdr
->p_filesz
;
191 tmp
= realloc(buf
, buf_size
);
196 fseek(fp
, phdr
->p_offset
, SEEK_SET
);
197 if (fread(buf
, buf_size
, 1, fp
) != 1)
200 ret
= read_build_id(buf
, buf_size
, bf
, size
, need_swap
);
213 int sysfs__read_build_id(const char *filename
, void *build_id
, size_t size
)
221 fd
= open(filename
, O_RDONLY
);
225 if (fstat(fd
, &stbuf
) < 0)
228 buf_size
= stbuf
.st_size
;
229 buf
= malloc(buf_size
);
233 if (read(fd
, buf
, buf_size
) != (ssize_t
) buf_size
)
236 ret
= read_build_id(buf
, buf_size
, build_id
, size
, false);
244 int symsrc__init(struct symsrc
*ss
, struct dso
*dso __maybe_unused
,
246 enum dso_binary_type type
)
248 int fd
= open(name
, O_RDONLY
);
252 ss
->name
= strdup(name
);
264 bool symsrc__possibly_runtime(struct symsrc
*ss __maybe_unused
)
266 /* Assume all sym sources could be a runtime image. */
270 bool symsrc__has_symtab(struct symsrc
*ss __maybe_unused
)
275 void symsrc__destroy(struct symsrc
*ss
)
281 int dso__synthesize_plt_symbols(struct dso
*dso __maybe_unused
,
282 struct symsrc
*ss __maybe_unused
,
283 struct map
*map __maybe_unused
,
284 symbol_filter_t filter __maybe_unused
)
289 int dso__load_sym(struct dso
*dso
, struct map
*map __maybe_unused
,
291 struct symsrc
*runtime_ss __maybe_unused
,
292 symbol_filter_t filter __maybe_unused
,
293 int kmodule __maybe_unused
)
295 unsigned char *build_id
[BUILD_ID_SIZE
];
297 if (filename__read_build_id(ss
->name
, build_id
, BUILD_ID_SIZE
) > 0) {
298 dso__set_build_id(dso
, build_id
);
304 int file__read_maps(int fd __maybe_unused
, bool exe __maybe_unused
,
305 mapfn_t mapfn __maybe_unused
, void *data __maybe_unused
,
306 bool *is_64_bit __maybe_unused
)
311 void symbol__elf_init(void)