Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 1 | #include "symbol.h" |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 2 | #include "util.h" |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 3 | |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 4 | #include <stdio.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <string.h> |
| 7 | #include <byteswap.h> |
| 8 | #include <sys/stat.h> |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 9 | |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 10 | |
| 11 | static bool check_need_swap(int file_endian) |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 12 | { |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 13 | const int data = 1; |
| 14 | u8 *check = (u8 *)&data; |
| 15 | int host_endian; |
| 16 | |
| 17 | if (check[0] == 1) |
| 18 | host_endian = ELFDATA2LSB; |
| 19 | else |
| 20 | host_endian = ELFDATA2MSB; |
| 21 | |
| 22 | return host_endian != file_endian; |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 23 | } |
| 24 | |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 25 | #define NOTE_ALIGN(sz) (((sz) + 3) & ~3) |
| 26 | |
| 27 | #define NT_GNU_BUILD_ID 3 |
| 28 | |
| 29 | static int read_build_id(void *note_data, size_t note_len, void *bf, |
| 30 | size_t size, bool need_swap) |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 31 | { |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 32 | struct { |
| 33 | u32 n_namesz; |
| 34 | u32 n_descsz; |
| 35 | u32 n_type; |
| 36 | } *nhdr; |
| 37 | void *ptr; |
| 38 | |
| 39 | ptr = note_data; |
| 40 | while (ptr < (note_data + note_len)) { |
| 41 | const char *name; |
| 42 | size_t namesz, descsz; |
| 43 | |
| 44 | nhdr = ptr; |
| 45 | if (need_swap) { |
| 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); |
| 49 | } |
| 50 | |
| 51 | namesz = NOTE_ALIGN(nhdr->n_namesz); |
| 52 | descsz = NOTE_ALIGN(nhdr->n_descsz); |
| 53 | |
| 54 | ptr += sizeof(*nhdr); |
| 55 | name = ptr; |
| 56 | ptr += namesz; |
| 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); |
| 61 | memcpy(bf, ptr, sz); |
| 62 | memset(bf + sz, 0, size - sz); |
| 63 | return 0; |
| 64 | } |
| 65 | } |
| 66 | ptr += descsz; |
| 67 | } |
| 68 | |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 69 | return -1; |
| 70 | } |
| 71 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 72 | int filename__read_debuglink(const char *filename __maybe_unused, |
| 73 | char *debuglink __maybe_unused, |
| 74 | size_t size __maybe_unused) |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 75 | { |
| 76 | return -1; |
| 77 | } |
| 78 | |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 79 | /* |
| 80 | * Just try PT_NOTE header otherwise fails |
| 81 | */ |
| 82 | int filename__read_build_id(const char *filename, void *bf, size_t size) |
| 83 | { |
| 84 | FILE *fp; |
| 85 | int ret = -1; |
| 86 | bool need_swap = false; |
| 87 | u8 e_ident[EI_NIDENT]; |
| 88 | size_t buf_size; |
| 89 | void *buf; |
| 90 | int i; |
| 91 | |
| 92 | fp = fopen(filename, "r"); |
| 93 | if (fp == NULL) |
| 94 | return -1; |
| 95 | |
| 96 | if (fread(e_ident, sizeof(e_ident), 1, fp) != 1) |
| 97 | goto out; |
| 98 | |
| 99 | if (memcmp(e_ident, ELFMAG, SELFMAG) || |
| 100 | e_ident[EI_VERSION] != EV_CURRENT) |
| 101 | goto out; |
| 102 | |
| 103 | need_swap = check_need_swap(e_ident[EI_DATA]); |
| 104 | |
| 105 | /* for simplicity */ |
| 106 | fseek(fp, 0, SEEK_SET); |
| 107 | |
| 108 | if (e_ident[EI_CLASS] == ELFCLASS32) { |
| 109 | Elf32_Ehdr ehdr; |
| 110 | Elf32_Phdr *phdr; |
| 111 | |
| 112 | if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) |
| 113 | goto out; |
| 114 | |
| 115 | if (need_swap) { |
| 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); |
| 119 | } |
| 120 | |
| 121 | buf_size = ehdr.e_phentsize * ehdr.e_phnum; |
| 122 | buf = malloc(buf_size); |
| 123 | if (buf == NULL) |
| 124 | goto out; |
| 125 | |
| 126 | fseek(fp, ehdr.e_phoff, SEEK_SET); |
| 127 | if (fread(buf, buf_size, 1, fp) != 1) |
| 128 | goto out_free; |
| 129 | |
| 130 | for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) { |
| 131 | void *tmp; |
Mitchell Krome | 7ad74b4 | 2014-12-16 12:16:12 +1000 | [diff] [blame] | 132 | long offset; |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 133 | |
| 134 | if (need_swap) { |
| 135 | phdr->p_type = bswap_32(phdr->p_type); |
| 136 | phdr->p_offset = bswap_32(phdr->p_offset); |
| 137 | phdr->p_filesz = bswap_32(phdr->p_filesz); |
| 138 | } |
| 139 | |
| 140 | if (phdr->p_type != PT_NOTE) |
| 141 | continue; |
| 142 | |
| 143 | buf_size = phdr->p_filesz; |
Mitchell Krome | 7ad74b4 | 2014-12-16 12:16:12 +1000 | [diff] [blame] | 144 | offset = phdr->p_offset; |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 145 | tmp = realloc(buf, buf_size); |
| 146 | if (tmp == NULL) |
| 147 | goto out_free; |
| 148 | |
| 149 | buf = tmp; |
Mitchell Krome | 7ad74b4 | 2014-12-16 12:16:12 +1000 | [diff] [blame] | 150 | fseek(fp, offset, SEEK_SET); |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 151 | if (fread(buf, buf_size, 1, fp) != 1) |
| 152 | goto out_free; |
| 153 | |
| 154 | ret = read_build_id(buf, buf_size, bf, size, need_swap); |
| 155 | if (ret == 0) |
| 156 | ret = size; |
| 157 | break; |
| 158 | } |
| 159 | } else { |
| 160 | Elf64_Ehdr ehdr; |
| 161 | Elf64_Phdr *phdr; |
| 162 | |
| 163 | if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1) |
| 164 | goto out; |
| 165 | |
| 166 | if (need_swap) { |
| 167 | ehdr.e_phoff = bswap_64(ehdr.e_phoff); |
| 168 | ehdr.e_phentsize = bswap_16(ehdr.e_phentsize); |
| 169 | ehdr.e_phnum = bswap_16(ehdr.e_phnum); |
| 170 | } |
| 171 | |
| 172 | buf_size = ehdr.e_phentsize * ehdr.e_phnum; |
| 173 | buf = malloc(buf_size); |
| 174 | if (buf == NULL) |
| 175 | goto out; |
| 176 | |
| 177 | fseek(fp, ehdr.e_phoff, SEEK_SET); |
| 178 | if (fread(buf, buf_size, 1, fp) != 1) |
| 179 | goto out_free; |
| 180 | |
| 181 | for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) { |
| 182 | void *tmp; |
Mitchell Krome | 7ad74b4 | 2014-12-16 12:16:12 +1000 | [diff] [blame] | 183 | long offset; |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 184 | |
| 185 | if (need_swap) { |
| 186 | phdr->p_type = bswap_32(phdr->p_type); |
| 187 | phdr->p_offset = bswap_64(phdr->p_offset); |
| 188 | phdr->p_filesz = bswap_64(phdr->p_filesz); |
| 189 | } |
| 190 | |
| 191 | if (phdr->p_type != PT_NOTE) |
| 192 | continue; |
| 193 | |
| 194 | buf_size = phdr->p_filesz; |
Mitchell Krome | 7ad74b4 | 2014-12-16 12:16:12 +1000 | [diff] [blame] | 195 | offset = phdr->p_offset; |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 196 | tmp = realloc(buf, buf_size); |
| 197 | if (tmp == NULL) |
| 198 | goto out_free; |
| 199 | |
| 200 | buf = tmp; |
Mitchell Krome | 7ad74b4 | 2014-12-16 12:16:12 +1000 | [diff] [blame] | 201 | fseek(fp, offset, SEEK_SET); |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 202 | if (fread(buf, buf_size, 1, fp) != 1) |
| 203 | goto out_free; |
| 204 | |
| 205 | ret = read_build_id(buf, buf_size, bf, size, need_swap); |
| 206 | if (ret == 0) |
| 207 | ret = size; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | out_free: |
| 212 | free(buf); |
| 213 | out: |
| 214 | fclose(fp); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | int sysfs__read_build_id(const char *filename, void *build_id, size_t size) |
| 219 | { |
| 220 | int fd; |
| 221 | int ret = -1; |
| 222 | struct stat stbuf; |
| 223 | size_t buf_size; |
| 224 | void *buf; |
| 225 | |
| 226 | fd = open(filename, O_RDONLY); |
| 227 | if (fd < 0) |
| 228 | return -1; |
| 229 | |
| 230 | if (fstat(fd, &stbuf) < 0) |
| 231 | goto out; |
| 232 | |
| 233 | buf_size = stbuf.st_size; |
| 234 | buf = malloc(buf_size); |
| 235 | if (buf == NULL) |
| 236 | goto out; |
| 237 | |
| 238 | if (read(fd, buf, buf_size) != (ssize_t) buf_size) |
| 239 | goto out_free; |
| 240 | |
| 241 | ret = read_build_id(buf, buf_size, build_id, size, false); |
| 242 | out_free: |
| 243 | free(buf); |
| 244 | out: |
| 245 | close(fd); |
| 246 | return ret; |
| 247 | } |
| 248 | |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 249 | int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 250 | enum dso_binary_type type) |
| 251 | { |
| 252 | int fd = open(name, O_RDONLY); |
| 253 | if (fd < 0) |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 254 | goto out_errno; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 255 | |
| 256 | ss->name = strdup(name); |
| 257 | if (!ss->name) |
| 258 | goto out_close; |
| 259 | |
Adrian Hunter | 779e24e | 2013-12-04 16:23:01 +0200 | [diff] [blame] | 260 | ss->fd = fd; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 261 | ss->type = type; |
| 262 | |
| 263 | return 0; |
| 264 | out_close: |
| 265 | close(fd); |
Arnaldo Carvalho de Melo | 18425f1 | 2015-03-24 11:49:02 -0300 | [diff] [blame] | 266 | out_errno: |
| 267 | dso->load_errno = errno; |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 268 | return -1; |
| 269 | } |
| 270 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 271 | bool symsrc__possibly_runtime(struct symsrc *ss __maybe_unused) |
Cody P Schafer | 3aafe5a | 2012-08-10 15:23:02 -0700 | [diff] [blame] | 272 | { |
| 273 | /* Assume all sym sources could be a runtime image. */ |
| 274 | return true; |
| 275 | } |
| 276 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 277 | bool symsrc__has_symtab(struct symsrc *ss __maybe_unused) |
Cody P Schafer | d26cd12 | 2012-08-10 15:23:00 -0700 | [diff] [blame] | 278 | { |
| 279 | return false; |
| 280 | } |
| 281 | |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 282 | void symsrc__destroy(struct symsrc *ss) |
| 283 | { |
Arnaldo Carvalho de Melo | 74cf249 | 2013-12-27 16:55:14 -0300 | [diff] [blame] | 284 | zfree(&ss->name); |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 285 | close(ss->fd); |
| 286 | } |
| 287 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 288 | int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused, |
| 289 | struct symsrc *ss __maybe_unused, |
| 290 | struct map *map __maybe_unused, |
| 291 | symbol_filter_t filter __maybe_unused) |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 292 | { |
| 293 | return 0; |
| 294 | } |
| 295 | |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 296 | static int fd__is_64_bit(int fd) |
| 297 | { |
| 298 | u8 e_ident[EI_NIDENT]; |
| 299 | |
| 300 | if (lseek(fd, 0, SEEK_SET)) |
| 301 | return -1; |
| 302 | |
| 303 | if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident)) |
| 304 | return -1; |
| 305 | |
| 306 | if (memcmp(e_ident, ELFMAG, SELFMAG) || |
| 307 | e_ident[EI_VERSION] != EV_CURRENT) |
| 308 | return -1; |
| 309 | |
| 310 | return e_ident[EI_CLASS] == ELFCLASS64; |
| 311 | } |
| 312 | |
Adrian Hunter | 2b5b8bb | 2014-07-22 16:17:59 +0300 | [diff] [blame] | 313 | enum dso_type dso__type_fd(int fd) |
| 314 | { |
| 315 | Elf64_Ehdr ehdr; |
| 316 | int ret; |
| 317 | |
| 318 | ret = fd__is_64_bit(fd); |
| 319 | if (ret < 0) |
| 320 | return DSO__TYPE_UNKNOWN; |
| 321 | |
| 322 | if (ret) |
| 323 | return DSO__TYPE_64BIT; |
| 324 | |
| 325 | if (readn(fd, &ehdr, sizeof(ehdr)) != sizeof(ehdr)) |
| 326 | return DSO__TYPE_UNKNOWN; |
| 327 | |
| 328 | if (ehdr.e_machine == EM_X86_64) |
| 329 | return DSO__TYPE_X32BIT; |
| 330 | |
| 331 | return DSO__TYPE_32BIT; |
| 332 | } |
| 333 | |
Irina Tirdea | 1d037ca | 2012-09-11 01:15:03 +0300 | [diff] [blame] | 334 | int dso__load_sym(struct dso *dso, struct map *map __maybe_unused, |
| 335 | struct symsrc *ss, |
| 336 | struct symsrc *runtime_ss __maybe_unused, |
| 337 | symbol_filter_t filter __maybe_unused, |
| 338 | int kmodule __maybe_unused) |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 339 | { |
Dima Kogan | f2f3096 | 2015-09-07 17:30:28 -0700 | [diff] [blame] | 340 | unsigned char build_id[BUILD_ID_SIZE]; |
Adrian Hunter | c6d8f2a | 2014-07-14 13:02:41 +0300 | [diff] [blame] | 341 | int ret; |
| 342 | |
| 343 | ret = fd__is_64_bit(ss->fd); |
| 344 | if (ret >= 0) |
| 345 | dso->is_64_bit = ret; |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 346 | |
Cody P Schafer | b68e2f9 | 2012-08-10 15:22:57 -0700 | [diff] [blame] | 347 | if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) { |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 348 | dso__set_build_id(dso, build_id); |
Namhyung Kim | b691f64 | 2012-08-06 13:41:22 +0900 | [diff] [blame] | 349 | } |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
Adrian Hunter | 8e0cf96 | 2013-08-07 14:38:51 +0300 | [diff] [blame] | 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) |
| 356 | { |
| 357 | return -1; |
| 358 | } |
| 359 | |
Adrian Hunter | afba19d | 2013-10-09 15:01:12 +0300 | [diff] [blame] | 360 | int kcore_extract__create(struct kcore_extract *kce __maybe_unused) |
| 361 | { |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | void kcore_extract__delete(struct kcore_extract *kce __maybe_unused) |
| 366 | { |
| 367 | } |
| 368 | |
Adrian Hunter | fc1b691 | 2013-10-14 16:57:29 +0300 | [diff] [blame] | 369 | int kcore_copy(const char *from_dir __maybe_unused, |
| 370 | const char *to_dir __maybe_unused) |
| 371 | { |
| 372 | return -1; |
| 373 | } |
| 374 | |
Namhyung Kim | 393be2e | 2012-08-06 13:41:21 +0900 | [diff] [blame] | 375 | void symbol__elf_init(void) |
| 376 | { |
| 377 | } |