1393be2e3SNamhyung Kim #include "symbol.h" 274cf249dSArnaldo Carvalho de Melo #include "util.h" 3393be2e3SNamhyung Kim 4a43783aeSArnaldo Carvalho de Melo #include <errno.h> 5b691f643SNamhyung Kim #include <stdio.h> 6b691f643SNamhyung Kim #include <fcntl.h> 7b691f643SNamhyung Kim #include <string.h> 8b691f643SNamhyung Kim #include <byteswap.h> 9b691f643SNamhyung Kim #include <sys/stat.h> 10393be2e3SNamhyung Kim 11b691f643SNamhyung Kim 12b691f643SNamhyung Kim static bool check_need_swap(int file_endian) 13393be2e3SNamhyung Kim { 14b691f643SNamhyung Kim const int data = 1; 15b691f643SNamhyung Kim u8 *check = (u8 *)&data; 16b691f643SNamhyung Kim int host_endian; 17b691f643SNamhyung Kim 18b691f643SNamhyung Kim if (check[0] == 1) 19b691f643SNamhyung Kim host_endian = ELFDATA2LSB; 20b691f643SNamhyung Kim else 21b691f643SNamhyung Kim host_endian = ELFDATA2MSB; 22b691f643SNamhyung Kim 23b691f643SNamhyung Kim return host_endian != file_endian; 24393be2e3SNamhyung Kim } 25393be2e3SNamhyung Kim 26b691f643SNamhyung Kim #define NOTE_ALIGN(sz) (((sz) + 3) & ~3) 27b691f643SNamhyung Kim 28b691f643SNamhyung Kim #define NT_GNU_BUILD_ID 3 29b691f643SNamhyung Kim 30b691f643SNamhyung Kim static int read_build_id(void *note_data, size_t note_len, void *bf, 31b691f643SNamhyung Kim size_t size, bool need_swap) 32393be2e3SNamhyung Kim { 33b691f643SNamhyung Kim struct { 34b691f643SNamhyung Kim u32 n_namesz; 35b691f643SNamhyung Kim u32 n_descsz; 36b691f643SNamhyung Kim u32 n_type; 37b691f643SNamhyung Kim } *nhdr; 38b691f643SNamhyung Kim void *ptr; 39b691f643SNamhyung Kim 40b691f643SNamhyung Kim ptr = note_data; 41b691f643SNamhyung Kim while (ptr < (note_data + note_len)) { 42b691f643SNamhyung Kim const char *name; 43b691f643SNamhyung Kim size_t namesz, descsz; 44b691f643SNamhyung Kim 45b691f643SNamhyung Kim nhdr = ptr; 46b691f643SNamhyung Kim if (need_swap) { 47b691f643SNamhyung Kim nhdr->n_namesz = bswap_32(nhdr->n_namesz); 48b691f643SNamhyung Kim nhdr->n_descsz = bswap_32(nhdr->n_descsz); 49b691f643SNamhyung Kim nhdr->n_type = bswap_32(nhdr->n_type); 50b691f643SNamhyung Kim } 51b691f643SNamhyung Kim 52b691f643SNamhyung Kim namesz = NOTE_ALIGN(nhdr->n_namesz); 53b691f643SNamhyung Kim descsz = NOTE_ALIGN(nhdr->n_descsz); 54b691f643SNamhyung Kim 55b691f643SNamhyung Kim ptr += sizeof(*nhdr); 56b691f643SNamhyung Kim name = ptr; 57b691f643SNamhyung Kim ptr += namesz; 58b691f643SNamhyung Kim if (nhdr->n_type == NT_GNU_BUILD_ID && 59b691f643SNamhyung Kim nhdr->n_namesz == sizeof("GNU")) { 60b691f643SNamhyung Kim if (memcmp(name, "GNU", sizeof("GNU")) == 0) { 61b691f643SNamhyung Kim size_t sz = min(size, descsz); 62b691f643SNamhyung Kim memcpy(bf, ptr, sz); 63b691f643SNamhyung Kim memset(bf + sz, 0, size - sz); 64b691f643SNamhyung Kim return 0; 65b691f643SNamhyung Kim } 66b691f643SNamhyung Kim } 67b691f643SNamhyung Kim ptr += descsz; 68b691f643SNamhyung Kim } 69b691f643SNamhyung Kim 70393be2e3SNamhyung Kim return -1; 71393be2e3SNamhyung Kim } 72393be2e3SNamhyung Kim 731d037ca1SIrina Tirdea int filename__read_debuglink(const char *filename __maybe_unused, 741d037ca1SIrina Tirdea char *debuglink __maybe_unused, 751d037ca1SIrina Tirdea size_t size __maybe_unused) 76393be2e3SNamhyung Kim { 77393be2e3SNamhyung Kim return -1; 78393be2e3SNamhyung Kim } 79393be2e3SNamhyung Kim 80b691f643SNamhyung Kim /* 81b691f643SNamhyung Kim * Just try PT_NOTE header otherwise fails 82b691f643SNamhyung Kim */ 83b691f643SNamhyung Kim int filename__read_build_id(const char *filename, void *bf, size_t size) 84b691f643SNamhyung Kim { 85b691f643SNamhyung Kim FILE *fp; 86b691f643SNamhyung Kim int ret = -1; 87b691f643SNamhyung Kim bool need_swap = false; 88b691f643SNamhyung Kim u8 e_ident[EI_NIDENT]; 89b691f643SNamhyung Kim size_t buf_size; 90b691f643SNamhyung Kim void *buf; 91b691f643SNamhyung Kim int i; 92b691f643SNamhyung Kim 93b691f643SNamhyung Kim fp = fopen(filename, "r"); 94b691f643SNamhyung Kim if (fp == NULL) 95b691f643SNamhyung Kim return -1; 96b691f643SNamhyung Kim 97b691f643SNamhyung Kim if (fread(e_ident, sizeof(e_ident), 1, fp) != 1) 98b691f643SNamhyung Kim goto out; 99b691f643SNamhyung Kim 100b691f643SNamhyung Kim if (memcmp(e_ident, ELFMAG, SELFMAG) || 101b691f643SNamhyung Kim e_ident[EI_VERSION] != EV_CURRENT) 102b691f643SNamhyung Kim goto out; 103b691f643SNamhyung Kim 104b691f643SNamhyung Kim need_swap = check_need_swap(e_ident[EI_DATA]); 105b691f643SNamhyung Kim 106b691f643SNamhyung Kim /* for simplicity */ 107b691f643SNamhyung Kim fseek(fp, 0, SEEK_SET); 108b691f643SNamhyung Kim 109b691f643SNamhyung Kim if (e_ident[EI_CLASS] == ELFCLASS32) { 110b691f643SNamhyung Kim Elf32_Ehdr ehdr; 111b691f643SNamhyung Kim Elf32_Phdr *phdr; 112b691f643SNamhyung Kim 113b691f643SNamhyung Kim if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) 114b691f643SNamhyung Kim goto out; 115b691f643SNamhyung Kim 116b691f643SNamhyung Kim if (need_swap) { 117b691f643SNamhyung Kim ehdr.e_phoff = bswap_32(ehdr.e_phoff); 118b691f643SNamhyung Kim ehdr.e_phentsize = bswap_16(ehdr.e_phentsize); 119b691f643SNamhyung Kim ehdr.e_phnum = bswap_16(ehdr.e_phnum); 120b691f643SNamhyung Kim } 121b691f643SNamhyung Kim 122b691f643SNamhyung Kim buf_size = ehdr.e_phentsize * ehdr.e_phnum; 123b691f643SNamhyung Kim buf = malloc(buf_size); 124b691f643SNamhyung Kim if (buf == NULL) 125b691f643SNamhyung Kim goto out; 126b691f643SNamhyung Kim 127b691f643SNamhyung Kim fseek(fp, ehdr.e_phoff, SEEK_SET); 128b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 129b691f643SNamhyung Kim goto out_free; 130b691f643SNamhyung Kim 131b691f643SNamhyung Kim for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) { 132b691f643SNamhyung Kim void *tmp; 1337ad74b41SMitchell Krome long offset; 134b691f643SNamhyung Kim 135b691f643SNamhyung Kim if (need_swap) { 136b691f643SNamhyung Kim phdr->p_type = bswap_32(phdr->p_type); 137b691f643SNamhyung Kim phdr->p_offset = bswap_32(phdr->p_offset); 138b691f643SNamhyung Kim phdr->p_filesz = bswap_32(phdr->p_filesz); 139b691f643SNamhyung Kim } 140b691f643SNamhyung Kim 141b691f643SNamhyung Kim if (phdr->p_type != PT_NOTE) 142b691f643SNamhyung Kim continue; 143b691f643SNamhyung Kim 144b691f643SNamhyung Kim buf_size = phdr->p_filesz; 1457ad74b41SMitchell Krome offset = phdr->p_offset; 146b691f643SNamhyung Kim tmp = realloc(buf, buf_size); 147b691f643SNamhyung Kim if (tmp == NULL) 148b691f643SNamhyung Kim goto out_free; 149b691f643SNamhyung Kim 150b691f643SNamhyung Kim buf = tmp; 1517ad74b41SMitchell Krome fseek(fp, offset, SEEK_SET); 152b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 153b691f643SNamhyung Kim goto out_free; 154b691f643SNamhyung Kim 155b691f643SNamhyung Kim ret = read_build_id(buf, buf_size, bf, size, need_swap); 156b691f643SNamhyung Kim if (ret == 0) 157b691f643SNamhyung Kim ret = size; 158b691f643SNamhyung Kim break; 159b691f643SNamhyung Kim } 160b691f643SNamhyung Kim } else { 161b691f643SNamhyung Kim Elf64_Ehdr ehdr; 162b691f643SNamhyung Kim Elf64_Phdr *phdr; 163b691f643SNamhyung Kim 164b691f643SNamhyung Kim if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) 165b691f643SNamhyung Kim goto out; 166b691f643SNamhyung Kim 167b691f643SNamhyung Kim if (need_swap) { 168b691f643SNamhyung Kim ehdr.e_phoff = bswap_64(ehdr.e_phoff); 169b691f643SNamhyung Kim ehdr.e_phentsize = bswap_16(ehdr.e_phentsize); 170b691f643SNamhyung Kim ehdr.e_phnum = bswap_16(ehdr.e_phnum); 171b691f643SNamhyung Kim } 172b691f643SNamhyung Kim 173b691f643SNamhyung Kim buf_size = ehdr.e_phentsize * ehdr.e_phnum; 174b691f643SNamhyung Kim buf = malloc(buf_size); 175b691f643SNamhyung Kim if (buf == NULL) 176b691f643SNamhyung Kim goto out; 177b691f643SNamhyung Kim 178b691f643SNamhyung Kim fseek(fp, ehdr.e_phoff, SEEK_SET); 179b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 180b691f643SNamhyung Kim goto out_free; 181b691f643SNamhyung Kim 182b691f643SNamhyung Kim for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) { 183b691f643SNamhyung Kim void *tmp; 1847ad74b41SMitchell Krome long offset; 185b691f643SNamhyung Kim 186b691f643SNamhyung Kim if (need_swap) { 187b691f643SNamhyung Kim phdr->p_type = bswap_32(phdr->p_type); 188b691f643SNamhyung Kim phdr->p_offset = bswap_64(phdr->p_offset); 189b691f643SNamhyung Kim phdr->p_filesz = bswap_64(phdr->p_filesz); 190b691f643SNamhyung Kim } 191b691f643SNamhyung Kim 192b691f643SNamhyung Kim if (phdr->p_type != PT_NOTE) 193b691f643SNamhyung Kim continue; 194b691f643SNamhyung Kim 195b691f643SNamhyung Kim buf_size = phdr->p_filesz; 1967ad74b41SMitchell Krome offset = phdr->p_offset; 197b691f643SNamhyung Kim tmp = realloc(buf, buf_size); 198b691f643SNamhyung Kim if (tmp == NULL) 199b691f643SNamhyung Kim goto out_free; 200b691f643SNamhyung Kim 201b691f643SNamhyung Kim buf = tmp; 2027ad74b41SMitchell Krome fseek(fp, offset, SEEK_SET); 203b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 204b691f643SNamhyung Kim goto out_free; 205b691f643SNamhyung Kim 206b691f643SNamhyung Kim ret = read_build_id(buf, buf_size, bf, size, need_swap); 207b691f643SNamhyung Kim if (ret == 0) 208b691f643SNamhyung Kim ret = size; 209b691f643SNamhyung Kim break; 210b691f643SNamhyung Kim } 211b691f643SNamhyung Kim } 212b691f643SNamhyung Kim out_free: 213b691f643SNamhyung Kim free(buf); 214b691f643SNamhyung Kim out: 215b691f643SNamhyung Kim fclose(fp); 216b691f643SNamhyung Kim return ret; 217b691f643SNamhyung Kim } 218b691f643SNamhyung Kim 219b691f643SNamhyung Kim int sysfs__read_build_id(const char *filename, void *build_id, size_t size) 220b691f643SNamhyung Kim { 221b691f643SNamhyung Kim int fd; 222b691f643SNamhyung Kim int ret = -1; 223b691f643SNamhyung Kim struct stat stbuf; 224b691f643SNamhyung Kim size_t buf_size; 225b691f643SNamhyung Kim void *buf; 226b691f643SNamhyung Kim 227b691f643SNamhyung Kim fd = open(filename, O_RDONLY); 228b691f643SNamhyung Kim if (fd < 0) 229b691f643SNamhyung Kim return -1; 230b691f643SNamhyung Kim 231b691f643SNamhyung Kim if (fstat(fd, &stbuf) < 0) 232b691f643SNamhyung Kim goto out; 233b691f643SNamhyung Kim 234b691f643SNamhyung Kim buf_size = stbuf.st_size; 235b691f643SNamhyung Kim buf = malloc(buf_size); 236b691f643SNamhyung Kim if (buf == NULL) 237b691f643SNamhyung Kim goto out; 238b691f643SNamhyung Kim 239b691f643SNamhyung Kim if (read(fd, buf, buf_size) != (ssize_t) buf_size) 240b691f643SNamhyung Kim goto out_free; 241b691f643SNamhyung Kim 242b691f643SNamhyung Kim ret = read_build_id(buf, buf_size, build_id, size, false); 243b691f643SNamhyung Kim out_free: 244b691f643SNamhyung Kim free(buf); 245b691f643SNamhyung Kim out: 246b691f643SNamhyung Kim close(fd); 247b691f643SNamhyung Kim return ret; 248b691f643SNamhyung Kim } 249b691f643SNamhyung Kim 25018425f13SArnaldo Carvalho de Melo int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, 251b68e2f91SCody P Schafer enum dso_binary_type type) 252b68e2f91SCody P Schafer { 253b68e2f91SCody P Schafer int fd = open(name, O_RDONLY); 254b68e2f91SCody P Schafer if (fd < 0) 25518425f13SArnaldo Carvalho de Melo goto out_errno; 256b68e2f91SCody P Schafer 257b68e2f91SCody P Schafer ss->name = strdup(name); 258b68e2f91SCody P Schafer if (!ss->name) 259b68e2f91SCody P Schafer goto out_close; 260b68e2f91SCody P Schafer 261779e24e2SAdrian Hunter ss->fd = fd; 262b68e2f91SCody P Schafer ss->type = type; 263b68e2f91SCody P Schafer 264b68e2f91SCody P Schafer return 0; 265b68e2f91SCody P Schafer out_close: 266b68e2f91SCody P Schafer close(fd); 26718425f13SArnaldo Carvalho de Melo out_errno: 26818425f13SArnaldo Carvalho de Melo dso->load_errno = errno; 269b68e2f91SCody P Schafer return -1; 270b68e2f91SCody P Schafer } 271b68e2f91SCody P Schafer 2721d037ca1SIrina Tirdea bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused) 2733aafe5aeSCody P Schafer { 2743aafe5aeSCody P Schafer /* Assume all sym sources could be a runtime image. */ 2753aafe5aeSCody P Schafer return true; 2763aafe5aeSCody P Schafer } 2773aafe5aeSCody P Schafer 2781d037ca1SIrina Tirdea bool symsrc__has_symtab(struct symsrc *ss __maybe_unused) 279d26cd12bSCody P Schafer { 280d26cd12bSCody P Schafer return false; 281d26cd12bSCody P Schafer } 282d26cd12bSCody P Schafer 283b68e2f91SCody P Schafer void symsrc__destroy(struct symsrc *ss) 284b68e2f91SCody P Schafer { 28574cf249dSArnaldo Carvalho de Melo zfree(&ss->name); 286b68e2f91SCody P Schafer close(ss->fd); 287b68e2f91SCody P Schafer } 288b68e2f91SCody P Schafer 2891d037ca1SIrina Tirdea int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused, 2901d037ca1SIrina Tirdea struct symsrc *ss __maybe_unused, 291be39db9fSArnaldo Carvalho de Melo struct map *map __maybe_unused) 292393be2e3SNamhyung Kim { 293393be2e3SNamhyung Kim return 0; 294393be2e3SNamhyung Kim } 295393be2e3SNamhyung Kim 296c6d8f2a4SAdrian Hunter static int fd__is_64_bit(int fd) 297c6d8f2a4SAdrian Hunter { 298c6d8f2a4SAdrian Hunter u8 e_ident[EI_NIDENT]; 299c6d8f2a4SAdrian Hunter 300c6d8f2a4SAdrian Hunter if (lseek(fd, 0, SEEK_SET)) 301c6d8f2a4SAdrian Hunter return -1; 302c6d8f2a4SAdrian Hunter 303c6d8f2a4SAdrian Hunter if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) 304c6d8f2a4SAdrian Hunter return -1; 305c6d8f2a4SAdrian Hunter 306c6d8f2a4SAdrian Hunter if (memcmp(e_ident, ELFMAG, SELFMAG) || 307c6d8f2a4SAdrian Hunter e_ident[EI_VERSION] != EV_CURRENT) 308c6d8f2a4SAdrian Hunter return -1; 309c6d8f2a4SAdrian Hunter 310c6d8f2a4SAdrian Hunter return e_ident[EI_CLASS] == ELFCLASS64; 311c6d8f2a4SAdrian Hunter } 312c6d8f2a4SAdrian Hunter 3132b5b8bb2SAdrian Hunter enum dso_type dso__type_fd(int fd) 3142b5b8bb2SAdrian Hunter { 3152b5b8bb2SAdrian Hunter Elf64_Ehdr ehdr; 3162b5b8bb2SAdrian Hunter int ret; 3172b5b8bb2SAdrian Hunter 3182b5b8bb2SAdrian Hunter ret = fd__is_64_bit(fd); 3192b5b8bb2SAdrian Hunter if (ret < 0) 3202b5b8bb2SAdrian Hunter return DSO__TYPE_UNKNOWN; 3212b5b8bb2SAdrian Hunter 3222b5b8bb2SAdrian Hunter if (ret) 3232b5b8bb2SAdrian Hunter return DSO__TYPE_64BIT; 3242b5b8bb2SAdrian Hunter 3252b5b8bb2SAdrian Hunter if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) 3262b5b8bb2SAdrian Hunter return DSO__TYPE_UNKNOWN; 3272b5b8bb2SAdrian Hunter 3282b5b8bb2SAdrian Hunter if (ehdr.e_machine == EM_X86_64) 3292b5b8bb2SAdrian Hunter return DSO__TYPE_X32BIT; 3302b5b8bb2SAdrian Hunter 3312b5b8bb2SAdrian Hunter return DSO__TYPE_32BIT; 3322b5b8bb2SAdrian Hunter } 3332b5b8bb2SAdrian Hunter 3341d037ca1SIrina Tirdea int dso__load_sym(struct dso *dso, struct map *map __maybe_unused, 3351d037ca1SIrina Tirdea struct symsrc *ss, 3361d037ca1SIrina Tirdea struct symsrc *runtime_ss __maybe_unused, 3371d037ca1SIrina Tirdea int kmodule __maybe_unused) 338393be2e3SNamhyung Kim { 339f2f30968SDima Kogan unsigned char build_id[BUILD_ID_SIZE]; 340c6d8f2a4SAdrian Hunter int ret; 341c6d8f2a4SAdrian Hunter 342c6d8f2a4SAdrian Hunter ret = fd__is_64_bit(ss->fd); 343c6d8f2a4SAdrian Hunter if (ret >= 0) 344c6d8f2a4SAdrian Hunter dso->is_64_bit = ret; 345b691f643SNamhyung Kim 346b68e2f91SCody P Schafer if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) { 347b691f643SNamhyung Kim dso__set_build_id(dso, build_id); 348b691f643SNamhyung Kim } 349393be2e3SNamhyung Kim return 0; 350393be2e3SNamhyung Kim } 351393be2e3SNamhyung Kim 3528e0cf965SAdrian Hunter int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused, 3538e0cf965SAdrian Hunter mapfn_t mapfn __maybe_unused, void *data __maybe_unused, 3548e0cf965SAdrian Hunter bool *is_64_bit __maybe_unused) 3558e0cf965SAdrian Hunter { 3568e0cf965SAdrian Hunter return -1; 3578e0cf965SAdrian Hunter } 3588e0cf965SAdrian Hunter 359afba19d9SAdrian Hunter int kcore_extract__create(struct kcore_extract *kce __maybe_unused) 360afba19d9SAdrian Hunter { 361afba19d9SAdrian Hunter return -1; 362afba19d9SAdrian Hunter } 363afba19d9SAdrian Hunter 364afba19d9SAdrian Hunter void kcore_extract__delete(struct kcore_extract *kce __maybe_unused) 365afba19d9SAdrian Hunter { 366afba19d9SAdrian Hunter } 367afba19d9SAdrian Hunter 368fc1b691dSAdrian Hunter int kcore_copy(const char *from_dir __maybe_unused, 369fc1b691dSAdrian Hunter const char *to_dir __maybe_unused) 370fc1b691dSAdrian Hunter { 371fc1b691dSAdrian Hunter return -1; 372fc1b691dSAdrian Hunter } 373fc1b691dSAdrian Hunter 374393be2e3SNamhyung Kim void symbol__elf_init(void) 375393be2e3SNamhyung Kim { 376393be2e3SNamhyung Kim } 377a64489c5SJin Yao 378a64489c5SJin Yao char *dso__demangle_sym(struct dso *dso __maybe_unused, 379a64489c5SJin Yao int kmodule __maybe_unused, 380*80c345b2SMilian Wolff const char *elf_name __maybe_unused) 381a64489c5SJin Yao { 382a64489c5SJin Yao return NULL; 383a64489c5SJin Yao } 384