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