1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) 2 3 #ifndef _GNU_SOURCE 4 #define _GNU_SOURCE 5 #endif 6 #include <libelf.h> 7 #include <gelf.h> 8 #include <fcntl.h> 9 #include <linux/kernel.h> 10 11 #include "libbpf_internal.h" 12 #include "str_error.h" 13 14 #define STRERR_BUFSIZE 128 15 16 /* A SHT_GNU_versym section holds 16-bit words. This bit is set if 17 * the symbol is hidden and can only be seen when referenced using an 18 * explicit version number. This is a GNU extension. 19 */ 20 #define VERSYM_HIDDEN 0x8000 21 22 /* This is the mask for the rest of the data in a word read from a 23 * SHT_GNU_versym section. 24 */ 25 #define VERSYM_VERSION 0x7fff 26 27 int elf_open(const char *binary_path, struct elf_fd *elf_fd) 28 { 29 char errmsg[STRERR_BUFSIZE]; 30 int fd, ret; 31 Elf *elf; 32 33 if (elf_version(EV_CURRENT) == EV_NONE) { 34 pr_warn("elf: failed to init libelf for %s\n", binary_path); 35 return -LIBBPF_ERRNO__LIBELF; 36 } 37 fd = open(binary_path, O_RDONLY | O_CLOEXEC); 38 if (fd < 0) { 39 ret = -errno; 40 pr_warn("elf: failed to open %s: %s\n", binary_path, 41 libbpf_strerror_r(ret, errmsg, sizeof(errmsg))); 42 return ret; 43 } 44 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); 45 if (!elf) { 46 pr_warn("elf: could not read elf from %s: %s\n", binary_path, elf_errmsg(-1)); 47 close(fd); 48 return -LIBBPF_ERRNO__FORMAT; 49 } 50 elf_fd->fd = fd; 51 elf_fd->elf = elf; 52 return 0; 53 } 54 55 void elf_close(struct elf_fd *elf_fd) 56 { 57 if (!elf_fd) 58 return; 59 elf_end(elf_fd->elf); 60 close(elf_fd->fd); 61 } 62 63 /* Return next ELF section of sh_type after scn, or first of that type if scn is NULL. */ 64 static Elf_Scn *elf_find_next_scn_by_type(Elf *elf, int sh_type, Elf_Scn *scn) 65 { 66 while ((scn = elf_nextscn(elf, scn)) != NULL) { 67 GElf_Shdr sh; 68 69 if (!gelf_getshdr(scn, &sh)) 70 continue; 71 if (sh.sh_type == sh_type) 72 return scn; 73 } 74 return NULL; 75 } 76 77 struct elf_sym { 78 const char *name; 79 GElf_Sym sym; 80 GElf_Shdr sh; 81 int ver; 82 bool hidden; 83 }; 84 85 struct elf_sym_iter { 86 Elf *elf; 87 Elf_Data *syms; 88 Elf_Data *versyms; 89 Elf_Data *verdefs; 90 size_t nr_syms; 91 size_t strtabidx; 92 size_t verdef_strtabidx; 93 size_t next_sym_idx; 94 struct elf_sym sym; 95 int st_type; 96 }; 97 98 static int elf_sym_iter_new(struct elf_sym_iter *iter, 99 Elf *elf, const char *binary_path, 100 int sh_type, int st_type) 101 { 102 Elf_Scn *scn = NULL; 103 GElf_Ehdr ehdr; 104 GElf_Shdr sh; 105 106 memset(iter, 0, sizeof(*iter)); 107 108 if (!gelf_getehdr(elf, &ehdr)) { 109 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1)); 110 return -EINVAL; 111 } 112 113 scn = elf_find_next_scn_by_type(elf, sh_type, NULL); 114 if (!scn) { 115 pr_debug("elf: failed to find symbol table ELF sections in '%s'\n", 116 binary_path); 117 return -ENOENT; 118 } 119 120 if (!gelf_getshdr(scn, &sh)) 121 return -EINVAL; 122 123 iter->strtabidx = sh.sh_link; 124 iter->syms = elf_getdata(scn, 0); 125 if (!iter->syms) { 126 pr_warn("elf: failed to get symbols for symtab section in '%s': %s\n", 127 binary_path, elf_errmsg(-1)); 128 return -EINVAL; 129 } 130 iter->nr_syms = iter->syms->d_size / sh.sh_entsize; 131 iter->elf = elf; 132 iter->st_type = st_type; 133 134 /* Version symbol table is meaningful to dynsym only */ 135 if (sh_type != SHT_DYNSYM) 136 return 0; 137 138 scn = elf_find_next_scn_by_type(elf, SHT_GNU_versym, NULL); 139 if (!scn) 140 return 0; 141 iter->versyms = elf_getdata(scn, 0); 142 143 scn = elf_find_next_scn_by_type(elf, SHT_GNU_verdef, NULL); 144 if (!scn) 145 return 0; 146 147 iter->verdefs = elf_getdata(scn, 0); 148 if (!iter->verdefs || !gelf_getshdr(scn, &sh)) { 149 pr_warn("elf: failed to get verdef ELF section in '%s'\n", binary_path); 150 return -EINVAL; 151 } 152 iter->verdef_strtabidx = sh.sh_link; 153 154 return 0; 155 } 156 157 static struct elf_sym *elf_sym_iter_next(struct elf_sym_iter *iter) 158 { 159 struct elf_sym *ret = &iter->sym; 160 GElf_Sym *sym = &ret->sym; 161 const char *name = NULL; 162 GElf_Versym versym; 163 Elf_Scn *sym_scn; 164 size_t idx; 165 166 for (idx = iter->next_sym_idx; idx < iter->nr_syms; idx++) { 167 if (!gelf_getsym(iter->syms, idx, sym)) 168 continue; 169 if (GELF_ST_TYPE(sym->st_info) != iter->st_type) 170 continue; 171 name = elf_strptr(iter->elf, iter->strtabidx, sym->st_name); 172 if (!name) 173 continue; 174 sym_scn = elf_getscn(iter->elf, sym->st_shndx); 175 if (!sym_scn) 176 continue; 177 if (!gelf_getshdr(sym_scn, &ret->sh)) 178 continue; 179 180 iter->next_sym_idx = idx + 1; 181 ret->name = name; 182 ret->ver = 0; 183 ret->hidden = false; 184 185 if (iter->versyms) { 186 if (!gelf_getversym(iter->versyms, idx, &versym)) 187 continue; 188 ret->ver = versym & VERSYM_VERSION; 189 ret->hidden = versym & VERSYM_HIDDEN; 190 } 191 return ret; 192 } 193 194 return NULL; 195 } 196 197 static const char *elf_get_vername(struct elf_sym_iter *iter, int ver) 198 { 199 GElf_Verdaux verdaux; 200 GElf_Verdef verdef; 201 int offset; 202 203 if (!iter->verdefs) 204 return NULL; 205 206 offset = 0; 207 while (gelf_getverdef(iter->verdefs, offset, &verdef)) { 208 if (verdef.vd_ndx != ver) { 209 if (!verdef.vd_next) 210 break; 211 212 offset += verdef.vd_next; 213 continue; 214 } 215 216 if (!gelf_getverdaux(iter->verdefs, offset + verdef.vd_aux, &verdaux)) 217 break; 218 219 return elf_strptr(iter->elf, iter->verdef_strtabidx, verdaux.vda_name); 220 221 } 222 return NULL; 223 } 224 225 static bool symbol_match(struct elf_sym_iter *iter, int sh_type, struct elf_sym *sym, 226 const char *name, size_t name_len, const char *lib_ver) 227 { 228 const char *ver_name; 229 230 /* Symbols are in forms of func, func@LIB_VER or func@@LIB_VER 231 * make sure the func part matches the user specified name 232 */ 233 if (strncmp(sym->name, name, name_len) != 0) 234 return false; 235 236 /* ...but we don't want a search for "foo" to match 'foo2" also, so any 237 * additional characters in sname should be of the form "@@LIB". 238 */ 239 if (sym->name[name_len] != '\0' && sym->name[name_len] != '@') 240 return false; 241 242 /* If user does not specify symbol version, then we got a match */ 243 if (!lib_ver) 244 return true; 245 246 /* If user specifies symbol version, for dynamic symbols, 247 * get version name from ELF verdef section for comparison. 248 */ 249 if (sh_type == SHT_DYNSYM) { 250 ver_name = elf_get_vername(iter, sym->ver); 251 if (!ver_name) 252 return false; 253 return strcmp(ver_name, lib_ver) == 0; 254 } 255 256 /* For normal symbols, it is already in form of func@LIB_VER */ 257 return strcmp(sym->name, name) == 0; 258 } 259 260 /* Transform symbol's virtual address (absolute for binaries and relative 261 * for shared libs) into file offset, which is what kernel is expecting 262 * for uprobe/uretprobe attachment. 263 * See Documentation/trace/uprobetracer.rst for more details. This is done 264 * by looking up symbol's containing section's header and using iter's virtual 265 * address (sh_addr) and corresponding file offset (sh_offset) to transform 266 * sym.st_value (virtual address) into desired final file offset. 267 */ 268 static unsigned long elf_sym_offset(struct elf_sym *sym) 269 { 270 return sym->sym.st_value - sym->sh.sh_addr + sym->sh.sh_offset; 271 } 272 273 /* Find offset of function name in the provided ELF object. "binary_path" is 274 * the path to the ELF binary represented by "elf", and only used for error 275 * reporting matters. "name" matches symbol name or name@@LIB for library 276 * functions. 277 */ 278 long elf_find_func_offset(Elf *elf, const char *binary_path, const char *name) 279 { 280 int i, sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB }; 281 const char *at_symbol, *lib_ver; 282 bool is_shared_lib; 283 long ret = -ENOENT; 284 size_t name_len; 285 GElf_Ehdr ehdr; 286 287 if (!gelf_getehdr(elf, &ehdr)) { 288 pr_warn("elf: failed to get ehdr from %s: %s\n", binary_path, elf_errmsg(-1)); 289 ret = -LIBBPF_ERRNO__FORMAT; 290 goto out; 291 } 292 /* for shared lib case, we do not need to calculate relative offset */ 293 is_shared_lib = ehdr.e_type == ET_DYN; 294 295 /* Does name specify "@@LIB_VER" or "@LIB_VER" ? */ 296 at_symbol = strchr(name, '@'); 297 if (at_symbol) { 298 name_len = at_symbol - name; 299 /* skip second @ if it's @@LIB_VER case */ 300 if (at_symbol[1] == '@') 301 at_symbol++; 302 lib_ver = at_symbol + 1; 303 } else { 304 name_len = strlen(name); 305 lib_ver = NULL; 306 } 307 308 /* Search SHT_DYNSYM, SHT_SYMTAB for symbol. This search order is used because if 309 * a binary is stripped, it may only have SHT_DYNSYM, and a fully-statically 310 * linked binary may not have SHT_DYMSYM, so absence of a section should not be 311 * reported as a warning/error. 312 */ 313 for (i = 0; i < ARRAY_SIZE(sh_types); i++) { 314 struct elf_sym_iter iter; 315 struct elf_sym *sym; 316 int last_bind = -1; 317 int cur_bind; 318 319 ret = elf_sym_iter_new(&iter, elf, binary_path, sh_types[i], STT_FUNC); 320 if (ret == -ENOENT) 321 continue; 322 if (ret) 323 goto out; 324 325 while ((sym = elf_sym_iter_next(&iter))) { 326 if (!symbol_match(&iter, sh_types[i], sym, name, name_len, lib_ver)) 327 continue; 328 329 cur_bind = GELF_ST_BIND(sym->sym.st_info); 330 331 if (ret > 0) { 332 /* handle multiple matches */ 333 if (elf_sym_offset(sym) == ret) { 334 /* same offset, no problem */ 335 continue; 336 } else if (last_bind != STB_WEAK && cur_bind != STB_WEAK) { 337 /* Only accept one non-weak bind. */ 338 pr_warn("elf: ambiguous match for '%s', '%s' in '%s'\n", 339 sym->name, name, binary_path); 340 ret = -LIBBPF_ERRNO__FORMAT; 341 goto out; 342 } else if (cur_bind == STB_WEAK) { 343 /* already have a non-weak bind, and 344 * this is a weak bind, so ignore. 345 */ 346 continue; 347 } 348 } 349 350 ret = elf_sym_offset(sym); 351 last_bind = cur_bind; 352 } 353 if (ret > 0) 354 break; 355 } 356 357 if (ret > 0) { 358 pr_debug("elf: symbol address match for '%s' in '%s': 0x%lx\n", name, binary_path, 359 ret); 360 } else { 361 if (ret == 0) { 362 pr_warn("elf: '%s' is 0 in symtab for '%s': %s\n", name, binary_path, 363 is_shared_lib ? "should not be 0 in a shared library" : 364 "try using shared library path instead"); 365 ret = -ENOENT; 366 } else { 367 pr_warn("elf: failed to find symbol '%s' in '%s'\n", name, binary_path); 368 } 369 } 370 out: 371 return ret; 372 } 373 374 /* Find offset of function name in ELF object specified by path. "name" matches 375 * symbol name or name@@LIB for library functions. 376 */ 377 long elf_find_func_offset_from_file(const char *binary_path, const char *name) 378 { 379 struct elf_fd elf_fd; 380 long ret = -ENOENT; 381 382 ret = elf_open(binary_path, &elf_fd); 383 if (ret) 384 return ret; 385 ret = elf_find_func_offset(elf_fd.elf, binary_path, name); 386 elf_close(&elf_fd); 387 return ret; 388 } 389 390 struct symbol { 391 const char *name; 392 int bind; 393 int idx; 394 }; 395 396 static int symbol_cmp(const void *a, const void *b) 397 { 398 const struct symbol *sym_a = a; 399 const struct symbol *sym_b = b; 400 401 return strcmp(sym_a->name, sym_b->name); 402 } 403 404 /* 405 * Return offsets in @poffsets for symbols specified in @syms array argument. 406 * On success returns 0 and offsets are returned in allocated array with @cnt 407 * size, that needs to be released by the caller. 408 */ 409 int elf_resolve_syms_offsets(const char *binary_path, int cnt, 410 const char **syms, unsigned long **poffsets) 411 { 412 int sh_types[2] = { SHT_DYNSYM, SHT_SYMTAB }; 413 int err = 0, i, cnt_done = 0; 414 unsigned long *offsets; 415 struct symbol *symbols; 416 struct elf_fd elf_fd; 417 418 err = elf_open(binary_path, &elf_fd); 419 if (err) 420 return err; 421 422 offsets = calloc(cnt, sizeof(*offsets)); 423 symbols = calloc(cnt, sizeof(*symbols)); 424 425 if (!offsets || !symbols) { 426 err = -ENOMEM; 427 goto out; 428 } 429 430 for (i = 0; i < cnt; i++) { 431 symbols[i].name = syms[i]; 432 symbols[i].idx = i; 433 } 434 435 qsort(symbols, cnt, sizeof(*symbols), symbol_cmp); 436 437 for (i = 0; i < ARRAY_SIZE(sh_types); i++) { 438 struct elf_sym_iter iter; 439 struct elf_sym *sym; 440 441 err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC); 442 if (err == -ENOENT) 443 continue; 444 if (err) 445 goto out; 446 447 while ((sym = elf_sym_iter_next(&iter))) { 448 unsigned long sym_offset = elf_sym_offset(sym); 449 int bind = GELF_ST_BIND(sym->sym.st_info); 450 struct symbol *found, tmp = { 451 .name = sym->name, 452 }; 453 unsigned long *offset; 454 455 found = bsearch(&tmp, symbols, cnt, sizeof(*symbols), symbol_cmp); 456 if (!found) 457 continue; 458 459 offset = &offsets[found->idx]; 460 if (*offset > 0) { 461 /* same offset, no problem */ 462 if (*offset == sym_offset) 463 continue; 464 /* handle multiple matches */ 465 if (found->bind != STB_WEAK && bind != STB_WEAK) { 466 /* Only accept one non-weak bind. */ 467 pr_warn("elf: ambiguous match found '%s@%lu' in '%s' previous offset %lu\n", 468 sym->name, sym_offset, binary_path, *offset); 469 err = -ESRCH; 470 goto out; 471 } else if (bind == STB_WEAK) { 472 /* already have a non-weak bind, and 473 * this is a weak bind, so ignore. 474 */ 475 continue; 476 } 477 } else { 478 cnt_done++; 479 } 480 *offset = sym_offset; 481 found->bind = bind; 482 } 483 } 484 485 if (cnt != cnt_done) { 486 err = -ENOENT; 487 goto out; 488 } 489 490 *poffsets = offsets; 491 492 out: 493 free(symbols); 494 if (err) 495 free(offsets); 496 elf_close(&elf_fd); 497 return err; 498 } 499 500 /* 501 * Return offsets in @poffsets for symbols specified by @pattern argument. 502 * On success returns 0 and offsets are returned in allocated @poffsets 503 * array with the @pctn size, that needs to be released by the caller. 504 */ 505 int elf_resolve_pattern_offsets(const char *binary_path, const char *pattern, 506 unsigned long **poffsets, size_t *pcnt) 507 { 508 int sh_types[2] = { SHT_SYMTAB, SHT_DYNSYM }; 509 unsigned long *offsets = NULL; 510 size_t cap = 0, cnt = 0; 511 struct elf_fd elf_fd; 512 int err = 0, i; 513 514 err = elf_open(binary_path, &elf_fd); 515 if (err) 516 return err; 517 518 for (i = 0; i < ARRAY_SIZE(sh_types); i++) { 519 struct elf_sym_iter iter; 520 struct elf_sym *sym; 521 522 err = elf_sym_iter_new(&iter, elf_fd.elf, binary_path, sh_types[i], STT_FUNC); 523 if (err == -ENOENT) 524 continue; 525 if (err) 526 goto out; 527 528 while ((sym = elf_sym_iter_next(&iter))) { 529 if (!glob_match(sym->name, pattern)) 530 continue; 531 532 err = libbpf_ensure_mem((void **) &offsets, &cap, sizeof(*offsets), 533 cnt + 1); 534 if (err) 535 goto out; 536 537 offsets[cnt++] = elf_sym_offset(sym); 538 } 539 540 /* If we found anything in the first symbol section, 541 * do not search others to avoid duplicates. 542 */ 543 if (cnt) 544 break; 545 } 546 547 if (cnt) { 548 *poffsets = offsets; 549 *pcnt = cnt; 550 } else { 551 err = -ENOENT; 552 } 553 554 out: 555 if (err) 556 free(offsets); 557 elf_close(&elf_fd); 558 return err; 559 } 560