1b1d1b094SArnaldo Carvalho de Melo #include "dso.h" 2393be2e3SNamhyung Kim #include "symbol.h" 3b1d1b094SArnaldo Carvalho de Melo #include "symsrc.h" 4393be2e3SNamhyung Kim 5a43783aeSArnaldo Carvalho de Melo #include <errno.h> 668c0188eSArnaldo Carvalho de Melo #include <unistd.h> 7b691f643SNamhyung Kim #include <stdio.h> 8b691f643SNamhyung Kim #include <fcntl.h> 9b691f643SNamhyung Kim #include <string.h> 10215a0d30SArnaldo Carvalho de Melo #include <stdlib.h> 11b691f643SNamhyung Kim #include <byteswap.h> 12b691f643SNamhyung Kim #include <sys/stat.h> 137f7c536fSArnaldo Carvalho de Melo #include <linux/zalloc.h> 14fb71c86cSArnaldo Carvalho de Melo #include <internal/lib.h> 15b691f643SNamhyung Kim 16b691f643SNamhyung Kim static bool check_need_swap(int file_endian) 17393be2e3SNamhyung Kim { 18b691f643SNamhyung Kim const int data = 1; 19b691f643SNamhyung Kim u8 *check = (u8 *)&data; 20b691f643SNamhyung Kim int host_endian; 21b691f643SNamhyung Kim 22b691f643SNamhyung Kim if (check[0] == 1) 23b691f643SNamhyung Kim host_endian = ELFDATA2LSB; 24b691f643SNamhyung Kim else 25b691f643SNamhyung Kim host_endian = ELFDATA2MSB; 26b691f643SNamhyung Kim 27b691f643SNamhyung Kim return host_endian != file_endian; 28393be2e3SNamhyung Kim } 29393be2e3SNamhyung Kim 30b691f643SNamhyung Kim #define NOTE_ALIGN(sz) (((sz) + 3) & ~3) 31b691f643SNamhyung Kim 32b691f643SNamhyung Kim #define NT_GNU_BUILD_ID 3 33b691f643SNamhyung Kim 34f766819cSJiri Olsa static int read_build_id(void *note_data, size_t note_len, struct build_id *bid, 35f766819cSJiri Olsa bool need_swap) 36393be2e3SNamhyung Kim { 37f766819cSJiri Olsa size_t size = sizeof(bid->data); 38b691f643SNamhyung Kim struct { 39b691f643SNamhyung Kim u32 n_namesz; 40b691f643SNamhyung Kim u32 n_descsz; 41b691f643SNamhyung Kim u32 n_type; 42b691f643SNamhyung Kim } *nhdr; 43b691f643SNamhyung Kim void *ptr; 44b691f643SNamhyung Kim 45b691f643SNamhyung Kim ptr = note_data; 46b691f643SNamhyung Kim while (ptr < (note_data + note_len)) { 47b691f643SNamhyung Kim const char *name; 48b691f643SNamhyung Kim size_t namesz, descsz; 49b691f643SNamhyung Kim 50b691f643SNamhyung Kim nhdr = ptr; 51b691f643SNamhyung Kim if (need_swap) { 52b691f643SNamhyung Kim nhdr->n_namesz = bswap_32(nhdr->n_namesz); 53b691f643SNamhyung Kim nhdr->n_descsz = bswap_32(nhdr->n_descsz); 54b691f643SNamhyung Kim nhdr->n_type = bswap_32(nhdr->n_type); 55b691f643SNamhyung Kim } 56b691f643SNamhyung Kim 57b691f643SNamhyung Kim namesz = NOTE_ALIGN(nhdr->n_namesz); 58b691f643SNamhyung Kim descsz = NOTE_ALIGN(nhdr->n_descsz); 59b691f643SNamhyung Kim 60b691f643SNamhyung Kim ptr += sizeof(*nhdr); 61b691f643SNamhyung Kim name = ptr; 62b691f643SNamhyung Kim ptr += namesz; 63b691f643SNamhyung Kim if (nhdr->n_type == NT_GNU_BUILD_ID && 64b691f643SNamhyung Kim nhdr->n_namesz == sizeof("GNU")) { 65b691f643SNamhyung Kim if (memcmp(name, "GNU", sizeof("GNU")) == 0) { 66b691f643SNamhyung Kim size_t sz = min(size, descsz); 67f766819cSJiri Olsa memcpy(bid->data, ptr, sz); 68f766819cSJiri Olsa memset(bid->data + sz, 0, size - sz); 69f766819cSJiri Olsa bid->size = sz; 70b691f643SNamhyung Kim return 0; 71b691f643SNamhyung Kim } 72b691f643SNamhyung Kim } 73b691f643SNamhyung Kim ptr += descsz; 74b691f643SNamhyung Kim } 75b691f643SNamhyung Kim 76393be2e3SNamhyung Kim return -1; 77393be2e3SNamhyung Kim } 78393be2e3SNamhyung Kim 791d037ca1SIrina Tirdea int filename__read_debuglink(const char *filename __maybe_unused, 801d037ca1SIrina Tirdea char *debuglink __maybe_unused, 811d037ca1SIrina Tirdea size_t size __maybe_unused) 82393be2e3SNamhyung Kim { 83393be2e3SNamhyung Kim return -1; 84393be2e3SNamhyung Kim } 85393be2e3SNamhyung Kim 86b691f643SNamhyung Kim /* 87b691f643SNamhyung Kim * Just try PT_NOTE header otherwise fails 88b691f643SNamhyung Kim */ 89f766819cSJiri Olsa int filename__read_build_id(const char *filename, struct build_id *bid) 90b691f643SNamhyung Kim { 91b691f643SNamhyung Kim FILE *fp; 92b691f643SNamhyung Kim int ret = -1; 93b691f643SNamhyung Kim bool need_swap = false; 94b691f643SNamhyung Kim u8 e_ident[EI_NIDENT]; 95b691f643SNamhyung Kim size_t buf_size; 96b691f643SNamhyung Kim void *buf; 97b691f643SNamhyung Kim int i; 98b691f643SNamhyung Kim 99b691f643SNamhyung Kim fp = fopen(filename, "r"); 100b691f643SNamhyung Kim if (fp == NULL) 101b691f643SNamhyung Kim return -1; 102b691f643SNamhyung Kim 103b691f643SNamhyung Kim if (fread(e_ident, sizeof(e_ident), 1, fp) != 1) 104b691f643SNamhyung Kim goto out; 105b691f643SNamhyung Kim 106b691f643SNamhyung Kim if (memcmp(e_ident, ELFMAG, SELFMAG) || 107b691f643SNamhyung Kim e_ident[EI_VERSION] != EV_CURRENT) 108b691f643SNamhyung Kim goto out; 109b691f643SNamhyung Kim 110b691f643SNamhyung Kim need_swap = check_need_swap(e_ident[EI_DATA]); 111b691f643SNamhyung Kim 112b691f643SNamhyung Kim /* for simplicity */ 113b691f643SNamhyung Kim fseek(fp, 0, SEEK_SET); 114b691f643SNamhyung Kim 115b691f643SNamhyung Kim if (e_ident[EI_CLASS] == ELFCLASS32) { 116b691f643SNamhyung Kim Elf32_Ehdr ehdr; 117b691f643SNamhyung Kim Elf32_Phdr *phdr; 118b691f643SNamhyung Kim 119b691f643SNamhyung Kim if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) 120b691f643SNamhyung Kim goto out; 121b691f643SNamhyung Kim 122b691f643SNamhyung Kim if (need_swap) { 123b691f643SNamhyung Kim ehdr.e_phoff = bswap_32(ehdr.e_phoff); 124b691f643SNamhyung Kim ehdr.e_phentsize = bswap_16(ehdr.e_phentsize); 125b691f643SNamhyung Kim ehdr.e_phnum = bswap_16(ehdr.e_phnum); 126b691f643SNamhyung Kim } 127b691f643SNamhyung Kim 128b691f643SNamhyung Kim buf_size = ehdr.e_phentsize * ehdr.e_phnum; 129b691f643SNamhyung Kim buf = malloc(buf_size); 130b691f643SNamhyung Kim if (buf == NULL) 131b691f643SNamhyung Kim goto out; 132b691f643SNamhyung Kim 133b691f643SNamhyung Kim fseek(fp, ehdr.e_phoff, SEEK_SET); 134b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 135b691f643SNamhyung Kim goto out_free; 136b691f643SNamhyung Kim 137b691f643SNamhyung Kim for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) { 138b691f643SNamhyung Kim void *tmp; 1397ad74b41SMitchell Krome long offset; 140b691f643SNamhyung Kim 141b691f643SNamhyung Kim if (need_swap) { 142b691f643SNamhyung Kim phdr->p_type = bswap_32(phdr->p_type); 143b691f643SNamhyung Kim phdr->p_offset = bswap_32(phdr->p_offset); 144b691f643SNamhyung Kim phdr->p_filesz = bswap_32(phdr->p_filesz); 145b691f643SNamhyung Kim } 146b691f643SNamhyung Kim 147b691f643SNamhyung Kim if (phdr->p_type != PT_NOTE) 148b691f643SNamhyung Kim continue; 149b691f643SNamhyung Kim 150b691f643SNamhyung Kim buf_size = phdr->p_filesz; 1517ad74b41SMitchell Krome offset = phdr->p_offset; 152b691f643SNamhyung Kim tmp = realloc(buf, buf_size); 153b691f643SNamhyung Kim if (tmp == NULL) 154b691f643SNamhyung Kim goto out_free; 155b691f643SNamhyung Kim 156b691f643SNamhyung Kim buf = tmp; 1577ad74b41SMitchell Krome fseek(fp, offset, SEEK_SET); 158b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 159b691f643SNamhyung Kim goto out_free; 160b691f643SNamhyung Kim 161f766819cSJiri Olsa ret = read_build_id(buf, buf_size, bid, need_swap); 162d0acce68SChengen Du if (ret == 0) { 163f766819cSJiri Olsa ret = bid->size; 164b691f643SNamhyung Kim break; 165b691f643SNamhyung Kim } 166d0acce68SChengen Du } 167b691f643SNamhyung Kim } else { 168b691f643SNamhyung Kim Elf64_Ehdr ehdr; 169b691f643SNamhyung Kim Elf64_Phdr *phdr; 170b691f643SNamhyung Kim 171b691f643SNamhyung Kim if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) 172b691f643SNamhyung Kim goto out; 173b691f643SNamhyung Kim 174b691f643SNamhyung Kim if (need_swap) { 175b691f643SNamhyung Kim ehdr.e_phoff = bswap_64(ehdr.e_phoff); 176b691f643SNamhyung Kim ehdr.e_phentsize = bswap_16(ehdr.e_phentsize); 177b691f643SNamhyung Kim ehdr.e_phnum = bswap_16(ehdr.e_phnum); 178b691f643SNamhyung Kim } 179b691f643SNamhyung Kim 180b691f643SNamhyung Kim buf_size = ehdr.e_phentsize * ehdr.e_phnum; 181b691f643SNamhyung Kim buf = malloc(buf_size); 182b691f643SNamhyung Kim if (buf == NULL) 183b691f643SNamhyung Kim goto out; 184b691f643SNamhyung Kim 185b691f643SNamhyung Kim fseek(fp, ehdr.e_phoff, SEEK_SET); 186b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 187b691f643SNamhyung Kim goto out_free; 188b691f643SNamhyung Kim 189b691f643SNamhyung Kim for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) { 190b691f643SNamhyung Kim void *tmp; 1917ad74b41SMitchell Krome long offset; 192b691f643SNamhyung Kim 193b691f643SNamhyung Kim if (need_swap) { 194b691f643SNamhyung Kim phdr->p_type = bswap_32(phdr->p_type); 195b691f643SNamhyung Kim phdr->p_offset = bswap_64(phdr->p_offset); 196b691f643SNamhyung Kim phdr->p_filesz = bswap_64(phdr->p_filesz); 197b691f643SNamhyung Kim } 198b691f643SNamhyung Kim 199b691f643SNamhyung Kim if (phdr->p_type != PT_NOTE) 200b691f643SNamhyung Kim continue; 201b691f643SNamhyung Kim 202b691f643SNamhyung Kim buf_size = phdr->p_filesz; 2037ad74b41SMitchell Krome offset = phdr->p_offset; 204b691f643SNamhyung Kim tmp = realloc(buf, buf_size); 205b691f643SNamhyung Kim if (tmp == NULL) 206b691f643SNamhyung Kim goto out_free; 207b691f643SNamhyung Kim 208b691f643SNamhyung Kim buf = tmp; 2097ad74b41SMitchell Krome fseek(fp, offset, SEEK_SET); 210b691f643SNamhyung Kim if (fread(buf, buf_size, 1, fp) != 1) 211b691f643SNamhyung Kim goto out_free; 212b691f643SNamhyung Kim 213f766819cSJiri Olsa ret = read_build_id(buf, buf_size, bid, need_swap); 214d0acce68SChengen Du if (ret == 0) { 215f766819cSJiri Olsa ret = bid->size; 216b691f643SNamhyung Kim break; 217b691f643SNamhyung Kim } 218b691f643SNamhyung Kim } 219d0acce68SChengen Du } 220b691f643SNamhyung Kim out_free: 221b691f643SNamhyung Kim free(buf); 222b691f643SNamhyung Kim out: 223b691f643SNamhyung Kim fclose(fp); 224b691f643SNamhyung Kim return ret; 225b691f643SNamhyung Kim } 226b691f643SNamhyung Kim 2273ff1b8c8SJiri Olsa int sysfs__read_build_id(const char *filename, struct build_id *bid) 228b691f643SNamhyung Kim { 229b691f643SNamhyung Kim int fd; 230b691f643SNamhyung Kim int ret = -1; 231b691f643SNamhyung Kim struct stat stbuf; 232b691f643SNamhyung Kim size_t buf_size; 233b691f643SNamhyung Kim void *buf; 234b691f643SNamhyung Kim 235b691f643SNamhyung Kim fd = open(filename, O_RDONLY); 236b691f643SNamhyung Kim if (fd < 0) 237b691f643SNamhyung Kim return -1; 238b691f643SNamhyung Kim 239b691f643SNamhyung Kim if (fstat(fd, &stbuf) < 0) 240b691f643SNamhyung Kim goto out; 241b691f643SNamhyung Kim 242b691f643SNamhyung Kim buf_size = stbuf.st_size; 243b691f643SNamhyung Kim buf = malloc(buf_size); 244b691f643SNamhyung Kim if (buf == NULL) 245b691f643SNamhyung Kim goto out; 246b691f643SNamhyung Kim 247b691f643SNamhyung Kim if (read(fd, buf, buf_size) != (ssize_t) buf_size) 248b691f643SNamhyung Kim goto out_free; 249b691f643SNamhyung Kim 2503ff1b8c8SJiri Olsa ret = read_build_id(buf, buf_size, bid, false); 251b691f643SNamhyung Kim out_free: 252b691f643SNamhyung Kim free(buf); 253b691f643SNamhyung Kim out: 254b691f643SNamhyung Kim close(fd); 255b691f643SNamhyung Kim return ret; 256b691f643SNamhyung Kim } 257b691f643SNamhyung Kim 25818425f13SArnaldo Carvalho de Melo int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, 259b68e2f91SCody P Schafer enum dso_binary_type type) 260b68e2f91SCody P Schafer { 261b68e2f91SCody P Schafer int fd = open(name, O_RDONLY); 262b68e2f91SCody P Schafer if (fd < 0) 26318425f13SArnaldo Carvalho de Melo goto out_errno; 264b68e2f91SCody P Schafer 265b68e2f91SCody P Schafer ss->name = strdup(name); 266b68e2f91SCody P Schafer if (!ss->name) 267b68e2f91SCody P Schafer goto out_close; 268b68e2f91SCody P Schafer 269779e24e2SAdrian Hunter ss->fd = fd; 270b68e2f91SCody P Schafer ss->type = type; 271b68e2f91SCody P Schafer 272b68e2f91SCody P Schafer return 0; 273b68e2f91SCody P Schafer out_close: 274b68e2f91SCody P Schafer close(fd); 27518425f13SArnaldo Carvalho de Melo out_errno: 276*ee756ef7SIan Rogers RC_CHK_ACCESS(dso)->load_errno = errno; 277b68e2f91SCody P Schafer return -1; 278b68e2f91SCody P Schafer } 279b68e2f91SCody P Schafer 2801d037ca1SIrina Tirdea bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused) 2813aafe5aeSCody P Schafer { 2823aafe5aeSCody P Schafer /* Assume all sym sources could be a runtime image. */ 2833aafe5aeSCody P Schafer return true; 2843aafe5aeSCody P Schafer } 2853aafe5aeSCody P Schafer 2861d037ca1SIrina Tirdea bool symsrc__has_symtab(struct symsrc *ss __maybe_unused) 287d26cd12bSCody P Schafer { 288d26cd12bSCody P Schafer return false; 289d26cd12bSCody P Schafer } 290d26cd12bSCody P Schafer 291b68e2f91SCody P Schafer void symsrc__destroy(struct symsrc *ss) 292b68e2f91SCody P Schafer { 29374cf249dSArnaldo Carvalho de Melo zfree(&ss->name); 294b68e2f91SCody P Schafer close(ss->fd); 295b68e2f91SCody P Schafer } 296b68e2f91SCody P Schafer 2971d037ca1SIrina Tirdea int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused, 2983183f8caSArnaldo Carvalho de Melo struct symsrc *ss __maybe_unused) 299393be2e3SNamhyung Kim { 300393be2e3SNamhyung Kim return 0; 301393be2e3SNamhyung Kim } 302393be2e3SNamhyung Kim 303c6d8f2a4SAdrian Hunter static int fd__is_64_bit(int fd) 304c6d8f2a4SAdrian Hunter { 305c6d8f2a4SAdrian Hunter u8 e_ident[EI_NIDENT]; 306c6d8f2a4SAdrian Hunter 307c6d8f2a4SAdrian Hunter if (lseek(fd, 0, SEEK_SET)) 308c6d8f2a4SAdrian Hunter return -1; 309c6d8f2a4SAdrian Hunter 310c6d8f2a4SAdrian Hunter if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) 311c6d8f2a4SAdrian Hunter return -1; 312c6d8f2a4SAdrian Hunter 313c6d8f2a4SAdrian Hunter if (memcmp(e_ident, ELFMAG, SELFMAG) || 314c6d8f2a4SAdrian Hunter e_ident[EI_VERSION] != EV_CURRENT) 315c6d8f2a4SAdrian Hunter return -1; 316c6d8f2a4SAdrian Hunter 317c6d8f2a4SAdrian Hunter return e_ident[EI_CLASS] == ELFCLASS64; 318c6d8f2a4SAdrian Hunter } 319c6d8f2a4SAdrian Hunter 3202b5b8bb2SAdrian Hunter enum dso_type dso__type_fd(int fd) 3212b5b8bb2SAdrian Hunter { 3222b5b8bb2SAdrian Hunter Elf64_Ehdr ehdr; 3232b5b8bb2SAdrian Hunter int ret; 3242b5b8bb2SAdrian Hunter 3252b5b8bb2SAdrian Hunter ret = fd__is_64_bit(fd); 3262b5b8bb2SAdrian Hunter if (ret < 0) 3272b5b8bb2SAdrian Hunter return DSO__TYPE_UNKNOWN; 3282b5b8bb2SAdrian Hunter 3292b5b8bb2SAdrian Hunter if (ret) 3302b5b8bb2SAdrian Hunter return DSO__TYPE_64BIT; 3312b5b8bb2SAdrian Hunter 3322b5b8bb2SAdrian Hunter if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) 3332b5b8bb2SAdrian Hunter return DSO__TYPE_UNKNOWN; 3342b5b8bb2SAdrian Hunter 3352b5b8bb2SAdrian Hunter if (ehdr.e_machine == EM_X86_64) 3362b5b8bb2SAdrian Hunter return DSO__TYPE_X32BIT; 3372b5b8bb2SAdrian Hunter 3382b5b8bb2SAdrian Hunter return DSO__TYPE_32BIT; 3392b5b8bb2SAdrian Hunter } 3402b5b8bb2SAdrian Hunter 3411d037ca1SIrina Tirdea int dso__load_sym(struct dso *dso, struct map *map __maybe_unused, 3421d037ca1SIrina Tirdea struct symsrc *ss, 3431d037ca1SIrina Tirdea struct symsrc *runtime_ss __maybe_unused, 3441d037ca1SIrina Tirdea int kmodule __maybe_unused) 345393be2e3SNamhyung Kim { 346f766819cSJiri Olsa struct build_id bid; 347c6d8f2a4SAdrian Hunter int ret; 348c6d8f2a4SAdrian Hunter 349c6d8f2a4SAdrian Hunter ret = fd__is_64_bit(ss->fd); 350c6d8f2a4SAdrian Hunter if (ret >= 0) 351*ee756ef7SIan Rogers RC_CHK_ACCESS(dso)->is_64_bit = ret; 352b691f643SNamhyung Kim 353f766819cSJiri Olsa if (filename__read_build_id(ss->name, &bid) > 0) 3548dfdf440SJiri Olsa dso__set_build_id(dso, &bid); 355393be2e3SNamhyung Kim return 0; 356393be2e3SNamhyung Kim } 357393be2e3SNamhyung Kim 3588e0cf965SAdrian Hunter int file__read_maps(int fd __maybe_unused, bool exe __maybe_unused, 3598e0cf965SAdrian Hunter mapfn_t mapfn __maybe_unused, void *data __maybe_unused, 3608e0cf965SAdrian Hunter bool *is_64_bit __maybe_unused) 3618e0cf965SAdrian Hunter { 3628e0cf965SAdrian Hunter return -1; 3638e0cf965SAdrian Hunter } 3648e0cf965SAdrian Hunter 365afba19d9SAdrian Hunter int kcore_extract__create(struct kcore_extract *kce __maybe_unused) 366afba19d9SAdrian Hunter { 367afba19d9SAdrian Hunter return -1; 368afba19d9SAdrian Hunter } 369afba19d9SAdrian Hunter 370afba19d9SAdrian Hunter void kcore_extract__delete(struct kcore_extract *kce __maybe_unused) 371afba19d9SAdrian Hunter { 372afba19d9SAdrian Hunter } 373afba19d9SAdrian Hunter 374fc1b691dSAdrian Hunter int kcore_copy(const char *from_dir __maybe_unused, 375fc1b691dSAdrian Hunter const char *to_dir __maybe_unused) 376fc1b691dSAdrian Hunter { 377fc1b691dSAdrian Hunter return -1; 378fc1b691dSAdrian Hunter } 379fc1b691dSAdrian Hunter 380393be2e3SNamhyung Kim void symbol__elf_init(void) 381393be2e3SNamhyung Kim { 382393be2e3SNamhyung Kim } 383a64489c5SJin Yao 384a64489c5SJin Yao char *dso__demangle_sym(struct dso *dso __maybe_unused, 385a64489c5SJin Yao int kmodule __maybe_unused, 38680c345b2SMilian Wolff const char *elf_name __maybe_unused) 387a64489c5SJin Yao { 388a64489c5SJin Yao return NULL; 389a64489c5SJin Yao } 39006ea72a4SNamhyung Kim 39106ea72a4SNamhyung Kim bool filename__has_section(const char *filename __maybe_unused, const char *sec __maybe_unused) 39206ea72a4SNamhyung Kim { 39306ea72a4SNamhyung Kim return false; 39406ea72a4SNamhyung Kim } 395