Lines Matching refs:obj
163 struct src_obj *obj);
164 static int linker_sanity_check_elf(struct src_obj *obj);
165 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
166 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec);
167 static int linker_sanity_check_btf(struct src_obj *obj);
168 static int linker_sanity_check_btf_ext(struct src_obj *obj);
169 static int linker_fixup_btf(struct src_obj *obj);
170 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj);
171 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj);
172 static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
174 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj);
175 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj);
176 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj);
444 struct src_obj obj = {}; in bpf_linker__add_file() local
453 err = err ?: linker_load_obj_file(linker, filename, opts, &obj); in bpf_linker__add_file()
454 err = err ?: linker_append_sec_data(linker, &obj); in bpf_linker__add_file()
455 err = err ?: linker_append_elf_syms(linker, &obj); in bpf_linker__add_file()
456 err = err ?: linker_append_elf_relos(linker, &obj); in bpf_linker__add_file()
457 err = err ?: linker_append_btf(linker, &obj); in bpf_linker__add_file()
458 err = err ?: linker_append_btf_ext(linker, &obj); in bpf_linker__add_file()
461 free(obj.btf_type_map); in bpf_linker__add_file()
462 btf__free(obj.btf); in bpf_linker__add_file()
463 btf_ext__free(obj.btf_ext); in bpf_linker__add_file()
464 free(obj.secs); in bpf_linker__add_file()
465 free(obj.sym_map); in bpf_linker__add_file()
466 if (obj.elf) in bpf_linker__add_file()
467 elf_end(obj.elf); in bpf_linker__add_file()
468 if (obj.fd >= 0) in bpf_linker__add_file()
469 close(obj.fd); in bpf_linker__add_file()
517 static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name) in add_src_sec() argument
519 struct src_sec *secs = obj->secs, *sec; in add_src_sec()
520 size_t new_cnt = obj->sec_cnt ? obj->sec_cnt + 1 : 2; in add_src_sec()
527 memset(secs + obj->sec_cnt, 0, (new_cnt - obj->sec_cnt) * sizeof(*secs)); in add_src_sec()
529 obj->secs = secs; in add_src_sec()
530 obj->sec_cnt = new_cnt; in add_src_sec()
532 sec = &obj->secs[new_cnt - 1]; in add_src_sec()
541 struct src_obj *obj) in linker_load_obj_file() argument
559 obj->filename = filename; in linker_load_obj_file()
561 obj->fd = open(filename, O_RDONLY); in linker_load_obj_file()
562 if (obj->fd < 0) { in linker_load_obj_file()
567 obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL); in linker_load_obj_file()
568 if (!obj->elf) { in linker_load_obj_file()
575 ehdr = elf64_getehdr(obj->elf); in linker_load_obj_file()
594 if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) { in linker_load_obj_file()
601 while ((scn = elf_nextscn(obj->elf, scn)) != NULL) { in linker_load_obj_file()
613 sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name); in linker_load_obj_file()
629 sec = add_src_sec(obj, sec_name); in linker_load_obj_file()
645 if (obj->symtab_sec_idx) { in linker_load_obj_file()
650 obj->symtab_sec_idx = sec_idx; in linker_load_obj_file()
657 obj->btf = btf__new(data->d_buf, shdr->sh_size); in linker_load_obj_file()
658 err = libbpf_get_error(obj->btf); in linker_load_obj_file()
667 obj->btf_ext = btf_ext__new(data->d_buf, shdr->sh_size); in linker_load_obj_file()
668 err = libbpf_get_error(obj->btf_ext); in linker_load_obj_file()
693 err = err ?: linker_sanity_check_elf(obj); in linker_load_obj_file()
694 err = err ?: linker_sanity_check_btf(obj); in linker_load_obj_file()
695 err = err ?: linker_sanity_check_btf_ext(obj); in linker_load_obj_file()
696 err = err ?: linker_fixup_btf(obj); in linker_load_obj_file()
706 static int linker_sanity_check_elf(struct src_obj *obj) in linker_sanity_check_elf() argument
711 if (!obj->symtab_sec_idx) { in linker_sanity_check_elf()
712 pr_warn("ELF is missing SYMTAB section in %s\n", obj->filename); in linker_sanity_check_elf()
715 if (!obj->shstrs_sec_idx) { in linker_sanity_check_elf()
716 pr_warn("ELF is missing section headers STRTAB section in %s\n", obj->filename); in linker_sanity_check_elf()
720 for (i = 1; i < obj->sec_cnt; i++) { in linker_sanity_check_elf()
721 sec = &obj->secs[i]; in linker_sanity_check_elf()
724 pr_warn("ELF section #%zu has empty name in %s\n", sec->sec_idx, obj->filename); in linker_sanity_check_elf()
738 err = linker_sanity_check_elf_symtab(obj, sec); in linker_sanity_check_elf()
753 err = linker_sanity_check_elf_relos(obj, sec); in linker_sanity_check_elf()
761 sec->sec_idx, sec->sec_name, (size_t)sec->shdr->sh_type, obj->filename); in linker_sanity_check_elf()
769 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec) in linker_sanity_check_elf_symtab() argument
780 if (!sec->shdr->sh_link || sec->shdr->sh_link >= obj->sec_cnt) { in linker_sanity_check_elf_symtab()
782 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename); in linker_sanity_check_elf_symtab()
785 link_sec = &obj->secs[sec->shdr->sh_link]; in linker_sanity_check_elf_symtab()
788 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename); in linker_sanity_check_elf_symtab()
803 pr_warn("ELF sym #0 is invalid in %s\n", obj->filename); in linker_sanity_check_elf_symtab()
822 i, obj->filename); in linker_sanity_check_elf_symtab()
828 if (sym->st_shndx < SHN_LORESERVE && sym->st_shndx >= obj->sec_cnt) { in linker_sanity_check_elf_symtab()
830 i, sec->sec_idx, (size_t)sym->st_shndx, obj->filename); in linker_sanity_check_elf_symtab()
843 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec) in linker_sanity_check_elf_relos() argument
855 if (sec->shdr->sh_link != obj->symtab_sec_idx) { in linker_sanity_check_elf_relos()
857 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename); in linker_sanity_check_elf_relos()
862 if (!sec->shdr->sh_info || sec->shdr->sh_info >= obj->sec_cnt) { in linker_sanity_check_elf_relos()
864 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename); in linker_sanity_check_elf_relos()
867 link_sec = &obj->secs[sec->shdr->sh_info]; in linker_sanity_check_elf_relos()
873 sec->sec_idx, obj->filename); in linker_sanity_check_elf_relos()
884 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename); in linker_sanity_check_elf_relos()
891 sym_sec = &obj->secs[obj->symtab_sec_idx]; in linker_sanity_check_elf_relos()
899 i, sec->sec_idx, sym_type, obj->filename); in linker_sanity_check_elf_relos()
905 i, sec->sec_idx, sym_idx, obj->filename); in linker_sanity_check_elf_relos()
912 i, sec->sec_idx, sym_idx, obj->filename); in linker_sanity_check_elf_relos()
944 static int linker_sanity_check_btf(struct src_obj *obj) in linker_sanity_check_btf() argument
949 if (!obj->btf) in linker_sanity_check_btf()
952 n = btf__get_nr_types(obj->btf); in linker_sanity_check_btf()
954 t = btf_type_by_id(obj->btf, i); in linker_sanity_check_btf()
956 err = err ?: btf_type_visit_type_ids(t, check_btf_type_id, obj->btf); in linker_sanity_check_btf()
957 err = err ?: btf_type_visit_str_offs(t, check_btf_str_off, obj->btf); in linker_sanity_check_btf()
965 static int linker_sanity_check_btf_ext(struct src_obj *obj) in linker_sanity_check_btf_ext() argument
969 if (!obj->btf_ext) in linker_sanity_check_btf_ext()
973 if (!obj->btf) in linker_sanity_check_btf_ext()
976 err = err ?: btf_ext_visit_type_ids(obj->btf_ext, check_btf_type_id, obj->btf); in linker_sanity_check_btf_ext()
977 err = err ?: btf_ext_visit_str_offs(obj->btf_ext, check_btf_str_off, obj->btf); in linker_sanity_check_btf_ext()
1163 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj) in linker_append_sec_data() argument
1167 for (i = 1; i < obj->sec_cnt; i++) { in linker_append_sec_data()
1171 src_sec = &obj->secs[i]; in linker_append_sec_data()
1215 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj) in linker_append_elf_syms() argument
1217 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx]; in linker_append_elf_syms()
1223 obj->sym_map = calloc(n + 1, sizeof(*obj->sym_map)); in linker_append_elf_syms()
1224 if (!obj->sym_map) in linker_append_elf_syms()
1234 sym_name = elf_strptr(obj->elf, str_sec_idx, sym->st_name); in linker_append_elf_syms()
1236 pr_warn("can't fetch symbol name for symbol #%d in '%s'\n", i, obj->filename); in linker_append_elf_syms()
1240 err = linker_append_elf_sym(linker, obj, sym, sym_name, i); in linker_append_elf_syms()
1574 struct src_obj *obj, Elf64_Sym *sym, int btf_id) in glob_map_defs_match() argument
1581 t = btf__type_by_id(obj->btf, btf_id); in glob_map_defs_match()
1586 t = skip_mods_and_typedefs(obj->btf, t->type, NULL); in glob_map_defs_match()
1588 err = parse_btf_map_def(sym_name, obj->btf, t, true /*strict*/, &src_def, &src_inner_def); in glob_map_defs_match()
1609 obj->btf, &src_def, &src_inner_def); in glob_map_defs_match()
1614 struct src_obj *obj, Elf64_Sym *sym, size_t sym_idx, int btf_id) in glob_syms_match() argument
1626 src_t = btf__type_by_id(obj->btf, btf_id); in glob_syms_match()
1635 return glob_map_defs_match(sym_name, linker, glob_sym, obj, sym, btf_id); in glob_syms_match()
1638 linker->btf, glob_sym->btf_id, obj->btf, btf_id)) in glob_syms_match()
1650 static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name, in find_glob_sym_btf() argument
1658 if (!obj->btf) { in find_glob_sym_btf()
1659 pr_warn("failed to find BTF info for object '%s'\n", obj->filename); in find_glob_sym_btf()
1663 n = btf__get_nr_types(obj->btf); in find_glob_sym_btf()
1665 t = btf__type_by_id(obj->btf, i); in find_glob_sym_btf()
1671 name = btf__str_by_offset(obj->btf, t->name_off); in find_glob_sym_btf()
1685 t = btf__type_by_id(obj->btf, vi->type); in find_glob_sym_btf()
1686 name = btf__str_by_offset(obj->btf, t->name_off); in find_glob_sym_btf()
1719 static struct src_sec *find_src_sec_by_name(struct src_obj *obj, const char *sec_name) in find_src_sec_by_name() argument
1724 for (i = 1; i < obj->sec_cnt; i++) { in find_src_sec_by_name()
1725 sec = &obj->secs[i]; in find_src_sec_by_name()
1803 static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj, in linker_append_elf_sym() argument
1821 if (!obj->btf) { in linker_append_elf_sym()
1826 src_sec = &obj->secs[sym->st_shndx]; in linker_append_elf_sym()
1833 obj->sym_map[src_sym_idx] = dst_sec->sec_sym_idx; in linker_append_elf_sym()
1842 err = find_glob_sym_btf(obj, sym, sym_name, &btf_sec_id, &btf_id); in linker_append_elf_sym()
1850 t = btf__type_by_id(obj->btf, btf_sec_id); in linker_append_elf_sym()
1851 sec_name = btf__str_by_offset(obj->btf, t->name_off); in linker_append_elf_sym()
1860 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_elf_sym()
1875 obj->sym_map[src_sym_idx] = glob_sym->sym_idx; in linker_append_elf_sym()
1884 src_sym_idx, sym_name, obj->filename); in linker_append_elf_sym()
1888 if (!glob_syms_match(sym_name, linker, glob_sym, obj, sym, src_sym_idx, btf_id)) in linker_append_elf_sym()
1945 obj->btf, btf_id)) in linker_append_elf_sym()
1951 obj->sym_map[src_sym_idx] = glob_sym->sym_idx; in linker_append_elf_sym()
1971 obj->sym_map[src_sym_idx] = dst_sym_idx; in linker_append_elf_sym()
2001 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj) in linker_append_elf_relos() argument
2003 struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx]; in linker_append_elf_relos()
2007 for (i = 1; i < obj->sec_cnt; i++) { in linker_append_elf_relos()
2013 src_sec = &obj->secs[i]; in linker_append_elf_relos()
2018 src_linked_sec = &obj->secs[src_sec->shdr->sh_info]; in linker_append_elf_relos()
2064 dst_sym_idx = obj->sym_map[src_sym_idx]; in linker_append_elf_relos()
2071 struct src_sec *sec = &obj->secs[src_sym->st_shndx]; in linker_append_elf_relos()
2101 static Elf64_Sym *find_sym_by_name(struct src_obj *obj, size_t sec_idx, in find_sym_by_name() argument
2104 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx]; in find_sym_by_name()
2116 name = elf_strptr(obj->elf, str_sec_idx, sym->st_name); in find_sym_by_name()
2129 static int linker_fixup_btf(struct src_obj *obj) in linker_fixup_btf() argument
2135 if (!obj->btf) in linker_fixup_btf()
2138 n = btf__get_nr_types(obj->btf); in linker_fixup_btf()
2143 t = btf_type_by_id(obj->btf, i); in linker_fixup_btf()
2147 sec_name = btf__str_by_offset(obj->btf, t->name_off); in linker_fixup_btf()
2148 sec = find_src_sec_by_name(obj, sec_name); in linker_fixup_btf()
2181 sec = add_src_sec(obj, sec_name); in linker_fixup_btf()
2195 const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type); in linker_fixup_btf()
2196 const char *var_name = btf__str_by_offset(obj->btf, vt->name_off); in linker_fixup_btf()
2204 sym = find_sym_by_name(obj, sec->sec_idx, STT_OBJECT, var_name); in linker_fixup_btf()
2233 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj) in linker_append_btf() argument
2239 if (!obj->btf) in linker_append_btf()
2243 n = btf__get_nr_types(obj->btf); in linker_append_btf()
2245 obj->btf_type_map = calloc(n + 1, sizeof(int)); in linker_append_btf()
2246 if (!obj->btf_type_map) in linker_append_btf()
2252 t = btf__type_by_id(obj->btf, i); in linker_append_btf()
2260 name = btf__str_by_offset(obj->btf, t->name_off); in linker_append_btf()
2283 obj->btf_type_map[i] = glob_sym->btf_id; in linker_append_btf()
2288 id = btf__add_type(linker->btf, obj->btf, t); in linker_append_btf()
2290 pr_warn("failed to append BTF type #%d from file '%s'\n", i, obj->filename); in linker_append_btf()
2294 obj->btf_type_map[i] = id; in linker_append_btf()
2308 if (btf_type_visit_type_ids(dst_t, remap_type_id, obj->btf_type_map)) in linker_append_btf()
2322 glob_sym->underlying_btf_id = obj->btf_type_map[-glob_sym->underlying_btf_id]; in linker_append_btf()
2329 for (i = 1; i < obj->sec_cnt; i++) { in linker_append_btf()
2335 src_sec = &obj->secs[i]; in linker_append_btf()
2350 t = btf__type_by_id(obj->btf, src_sec->sec_type_id); in linker_append_btf()
2355 int new_id = obj->btf_type_map[src_var->type]; in linker_append_btf()
2402 dst_var->type = obj->btf_type_map[src_var->type]; in linker_append_btf()
2431 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj) in linker_append_btf_ext() argument
2439 if (!obj->btf_ext) in linker_append_btf_ext()
2442 rec_sz = obj->btf_ext->func_info.rec_size; in linker_append_btf_ext()
2443 for_each_btf_ext_sec(&obj->btf_ext->func_info, ext_sec) { in linker_append_btf_ext()
2446 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off); in linker_append_btf_ext()
2447 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_btf_ext()
2461 for_each_btf_ext_rec(&obj->btf_ext->func_info, ext_sec, i, src_rec) { in linker_append_btf_ext()
2467 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id]; in linker_append_btf_ext()
2471 rec_sz = obj->btf_ext->line_info.rec_size; in linker_append_btf_ext()
2472 for_each_btf_ext_sec(&obj->btf_ext->line_info, ext_sec) { in linker_append_btf_ext()
2475 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off); in linker_append_btf_ext()
2476 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_btf_ext()
2490 for_each_btf_ext_rec(&obj->btf_ext->line_info, ext_sec, i, src_rec) { in linker_append_btf_ext()
2497 s = btf__str_by_offset(obj->btf, src_rec->file_name_off); in linker_append_btf_ext()
2503 s = btf__str_by_offset(obj->btf, src_rec->line_off); in linker_append_btf_ext()
2513 rec_sz = obj->btf_ext->core_relo_info.rec_size; in linker_append_btf_ext()
2514 for_each_btf_ext_sec(&obj->btf_ext->core_relo_info, ext_sec) { in linker_append_btf_ext()
2517 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off); in linker_append_btf_ext()
2518 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_btf_ext()
2532 for_each_btf_ext_rec(&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) { in linker_append_btf_ext()
2538 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id]; in linker_append_btf_ext()
2540 s = btf__str_by_offset(obj->btf, src_rec->access_str_off); in linker_append_btf_ext()