Lines Matching refs:obj

173 				struct src_obj *obj);
174 static int linker_sanity_check_elf(struct src_obj *obj);
175 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
176 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec);
177 static int linker_sanity_check_btf(struct src_obj *obj);
178 static int linker_sanity_check_btf_ext(struct src_obj *obj);
179 static int linker_fixup_btf(struct src_obj *obj);
180 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj);
181 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj);
182 static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
184 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj);
185 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj);
186 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj);
494 struct src_obj obj = {}; in bpf_linker_add_file() local
497 obj.filename = filename; in bpf_linker_add_file()
498 obj.fd = fd; in bpf_linker_add_file()
500 err = err ?: linker_load_obj_file(linker, &obj); in bpf_linker_add_file()
501 err = err ?: linker_append_sec_data(linker, &obj); in bpf_linker_add_file()
502 err = err ?: linker_append_elf_syms(linker, &obj); in bpf_linker_add_file()
503 err = err ?: linker_append_elf_relos(linker, &obj); in bpf_linker_add_file()
504 err = err ?: linker_append_btf(linker, &obj); in bpf_linker_add_file()
505 err = err ?: linker_append_btf_ext(linker, &obj); in bpf_linker_add_file()
508 free(obj.btf_type_map); in bpf_linker_add_file()
509 btf__free(obj.btf); in bpf_linker_add_file()
510 btf_ext__free(obj.btf_ext); in bpf_linker_add_file()
511 free(obj.secs); in bpf_linker_add_file()
512 free(obj.sym_map); in bpf_linker_add_file()
513 if (obj.elf) in bpf_linker_add_file()
514 elf_end(obj.elf); in bpf_linker_add_file()
643 static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name) in add_src_sec() argument
645 struct src_sec *secs = obj->secs, *sec; in add_src_sec()
646 size_t new_cnt = obj->sec_cnt ? obj->sec_cnt + 1 : 2; in add_src_sec()
653 memset(secs + obj->sec_cnt, 0, (new_cnt - obj->sec_cnt) * sizeof(*secs)); in add_src_sec()
655 obj->secs = secs; in add_src_sec()
656 obj->sec_cnt = new_cnt; in add_src_sec()
658 sec = &obj->secs[new_cnt - 1]; in add_src_sec()
666 struct src_obj *obj) in linker_load_obj_file() argument
684 pr_debug("linker: adding object file '%s'...\n", obj->filename); in linker_load_obj_file()
686 obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL); in linker_load_obj_file()
687 if (!obj->elf) { in linker_load_obj_file()
688 pr_warn_elf("failed to parse ELF file '%s'", obj->filename); in linker_load_obj_file()
693 ehdr = elf64_getehdr(obj->elf); in linker_load_obj_file()
695 pr_warn_elf("failed to get ELF header for %s", obj->filename); in linker_load_obj_file()
703 pr_warn("unknown byte order of ELF file %s\n", obj->filename); in linker_load_obj_file()
713 pr_warn("byte order mismatch with ELF file %s\n", obj->filename); in linker_load_obj_file()
721 pr_warn_elf("unsupported kind of ELF file %s", obj->filename); in linker_load_obj_file()
725 if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) { in linker_load_obj_file()
726 pr_warn_elf("failed to get SHSTRTAB section index for %s", obj->filename); in linker_load_obj_file()
731 while ((scn = elf_nextscn(obj->elf, scn)) != NULL) { in linker_load_obj_file()
738 sec_idx, obj->filename); in linker_load_obj_file()
742 sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name); in linker_load_obj_file()
745 sec_idx, obj->filename); in linker_load_obj_file()
752 sec_idx, sec_name, obj->filename); in linker_load_obj_file()
756 sec = add_src_sec(obj, sec_name); in linker_load_obj_file()
772 if (obj->symtab_sec_idx) { in linker_load_obj_file()
777 obj->symtab_sec_idx = sec_idx; in linker_load_obj_file()
784 obj->btf = btf__new(data->d_buf, shdr->sh_size); in linker_load_obj_file()
785 err = libbpf_get_error(obj->btf); in linker_load_obj_file()
788 obj->filename, errstr(err)); in linker_load_obj_file()
795 obj->btf_ext = btf_ext__new(data->d_buf, shdr->sh_size); in linker_load_obj_file()
796 err = libbpf_get_error(obj->btf_ext); in linker_load_obj_file()
799 obj->filename, errstr(err)); in linker_load_obj_file()
816 sec_idx, sec_name, obj->filename); in linker_load_obj_file()
822 err = err ?: linker_sanity_check_elf(obj); in linker_load_obj_file()
823 err = err ?: linker_sanity_check_btf(obj); in linker_load_obj_file()
824 err = err ?: linker_sanity_check_btf_ext(obj); in linker_load_obj_file()
825 err = err ?: linker_fixup_btf(obj); in linker_load_obj_file()
830 static int linker_sanity_check_elf(struct src_obj *obj) in linker_sanity_check_elf() argument
835 if (!obj->symtab_sec_idx) { in linker_sanity_check_elf()
836 pr_warn("ELF is missing SYMTAB section in %s\n", obj->filename); in linker_sanity_check_elf()
839 if (!obj->shstrs_sec_idx) { in linker_sanity_check_elf()
840 pr_warn("ELF is missing section headers STRTAB section in %s\n", obj->filename); in linker_sanity_check_elf()
844 for (i = 1; i < obj->sec_cnt; i++) { in linker_sanity_check_elf()
845 sec = &obj->secs[i]; in linker_sanity_check_elf()
848 pr_warn("ELF section #%zu has empty name in %s\n", sec->sec_idx, obj->filename); in linker_sanity_check_elf()
858 obj->filename); in linker_sanity_check_elf()
864 (long long unsigned)sec->data->d_align, obj->filename); in linker_sanity_check_elf()
871 (long long unsigned)sec->data->d_size, obj->filename); in linker_sanity_check_elf()
877 err = linker_sanity_check_elf_symtab(obj, sec); in linker_sanity_check_elf()
888 obj->filename); in linker_sanity_check_elf()
896 err = linker_sanity_check_elf_relos(obj, sec); in linker_sanity_check_elf()
904 sec->sec_idx, sec->sec_name, (size_t)sec->shdr->sh_type, obj->filename); in linker_sanity_check_elf()
912 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec) in linker_sanity_check_elf_symtab() argument
923 if (!sec->shdr->sh_link || sec->shdr->sh_link >= obj->sec_cnt) { in linker_sanity_check_elf_symtab()
925 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename); in linker_sanity_check_elf_symtab()
928 link_sec = &obj->secs[sec->shdr->sh_link]; in linker_sanity_check_elf_symtab()
931 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename); in linker_sanity_check_elf_symtab()
946 pr_warn("ELF sym #0 is invalid in %s\n", obj->filename); in linker_sanity_check_elf_symtab()
965 i, obj->filename); in linker_sanity_check_elf_symtab()
971 if (sym->st_shndx < SHN_LORESERVE && sym->st_shndx >= obj->sec_cnt) { in linker_sanity_check_elf_symtab()
973 i, sec->sec_idx, (size_t)sym->st_shndx, obj->filename); in linker_sanity_check_elf_symtab()
986 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec) in linker_sanity_check_elf_relos() argument
998 if (sec->shdr->sh_link != obj->symtab_sec_idx) { in linker_sanity_check_elf_relos()
1000 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename); in linker_sanity_check_elf_relos()
1005 if (!sec->shdr->sh_info || sec->shdr->sh_info >= obj->sec_cnt) { in linker_sanity_check_elf_relos()
1007 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename); in linker_sanity_check_elf_relos()
1010 link_sec = &obj->secs[sec->shdr->sh_info]; in linker_sanity_check_elf_relos()
1016 sec->sec_idx, obj->filename); in linker_sanity_check_elf_relos()
1027 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename); in linker_sanity_check_elf_relos()
1034 sym_sec = &obj->secs[obj->symtab_sec_idx]; in linker_sanity_check_elf_relos()
1042 i, sec->sec_idx, sym_type, obj->filename); in linker_sanity_check_elf_relos()
1048 i, sec->sec_idx, sym_idx, obj->filename); in linker_sanity_check_elf_relos()
1055 i, sec->sec_idx, sym_idx, obj->filename); in linker_sanity_check_elf_relos()
1087 static int linker_sanity_check_btf(struct src_obj *obj) in linker_sanity_check_btf() argument
1092 if (!obj->btf) in linker_sanity_check_btf()
1095 n = btf__type_cnt(obj->btf); in linker_sanity_check_btf()
1100 t = btf_type_by_id(obj->btf, i); in linker_sanity_check_btf()
1114 if (!btf__str_by_offset(obj->btf, *str_off)) in linker_sanity_check_btf()
1122 static int linker_sanity_check_btf_ext(struct src_obj *obj) in linker_sanity_check_btf_ext() argument
1126 if (!obj->btf_ext) in linker_sanity_check_btf_ext()
1130 if (!obj->btf) in linker_sanity_check_btf_ext()
1133 err = err ?: btf_ext_visit_type_ids(obj->btf_ext, check_btf_type_id, obj->btf); in linker_sanity_check_btf_ext()
1134 err = err ?: btf_ext_visit_str_offs(obj->btf_ext, check_btf_str_off, obj->btf); in linker_sanity_check_btf_ext()
1354 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj) in linker_append_sec_data() argument
1358 for (i = 1; i < obj->sec_cnt; i++) { in linker_append_sec_data()
1362 src_sec = &obj->secs[i]; in linker_append_sec_data()
1406 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj) in linker_append_elf_syms() argument
1408 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx]; in linker_append_elf_syms()
1414 obj->sym_map = calloc(n + 1, sizeof(*obj->sym_map)); in linker_append_elf_syms()
1415 if (!obj->sym_map) in linker_append_elf_syms()
1425 sym_name = elf_strptr(obj->elf, str_sec_idx, sym->st_name); in linker_append_elf_syms()
1427 pr_warn("can't fetch symbol name for symbol #%d in '%s'\n", i, obj->filename); in linker_append_elf_syms()
1431 err = linker_append_elf_sym(linker, obj, sym, sym_name, i); in linker_append_elf_syms()
1767 struct src_obj *obj, Elf64_Sym *sym, int btf_id) in glob_map_defs_match() argument
1774 t = btf__type_by_id(obj->btf, btf_id); in glob_map_defs_match()
1779 t = skip_mods_and_typedefs(obj->btf, t->type, NULL); in glob_map_defs_match()
1781 err = parse_btf_map_def(sym_name, obj->btf, t, true /*strict*/, &src_def, &src_inner_def); in glob_map_defs_match()
1802 obj->btf, &src_def, &src_inner_def); in glob_map_defs_match()
1807 struct src_obj *obj, Elf64_Sym *sym, size_t sym_idx, int btf_id) in glob_syms_match() argument
1819 src_t = btf__type_by_id(obj->btf, btf_id); in glob_syms_match()
1828 return glob_map_defs_match(sym_name, linker, glob_sym, obj, sym, btf_id); in glob_syms_match()
1831 linker->btf, glob_sym->btf_id, obj->btf, btf_id)) in glob_syms_match()
1843 static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name, in find_glob_sym_btf() argument
1851 if (!obj->btf) { in find_glob_sym_btf()
1852 pr_warn("failed to find BTF info for object '%s'\n", obj->filename); in find_glob_sym_btf()
1856 n = btf__type_cnt(obj->btf); in find_glob_sym_btf()
1858 t = btf__type_by_id(obj->btf, i); in find_glob_sym_btf()
1864 name = btf__str_by_offset(obj->btf, t->name_off); in find_glob_sym_btf()
1878 t = btf__type_by_id(obj->btf, vi->type); in find_glob_sym_btf()
1879 name = btf__str_by_offset(obj->btf, t->name_off); in find_glob_sym_btf()
1912 static struct src_sec *find_src_sec_by_name(struct src_obj *obj, const char *sec_name) in find_src_sec_by_name() argument
1917 for (i = 1; i < obj->sec_cnt; i++) { in find_src_sec_by_name()
1918 sec = &obj->secs[i]; in find_src_sec_by_name()
1996 static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj, in linker_append_elf_sym() argument
2014 if (!obj->btf) { in linker_append_elf_sym()
2019 src_sec = &obj->secs[sym->st_shndx]; in linker_append_elf_sym()
2026 obj->sym_map[src_sym_idx] = dst_sec->sec_sym_idx; in linker_append_elf_sym()
2035 err = find_glob_sym_btf(obj, sym, sym_name, &btf_sec_id, &btf_id); in linker_append_elf_sym()
2043 t = btf__type_by_id(obj->btf, btf_sec_id); in linker_append_elf_sym()
2044 sec_name = btf__str_by_offset(obj->btf, t->name_off); in linker_append_elf_sym()
2053 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_elf_sym()
2068 obj->sym_map[src_sym_idx] = glob_sym->sym_idx; in linker_append_elf_sym()
2077 src_sym_idx, sym_name, obj->filename); in linker_append_elf_sym()
2081 if (!glob_syms_match(sym_name, linker, glob_sym, obj, sym, src_sym_idx, btf_id)) in linker_append_elf_sym()
2138 obj->btf, btf_id)) in linker_append_elf_sym()
2144 obj->sym_map[src_sym_idx] = glob_sym->sym_idx; in linker_append_elf_sym()
2164 obj->sym_map[src_sym_idx] = dst_sym_idx; in linker_append_elf_sym()
2194 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj) in linker_append_elf_relos() argument
2196 struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx]; in linker_append_elf_relos()
2199 for (i = 1; i < obj->sec_cnt; i++) { in linker_append_elf_relos()
2205 src_sec = &obj->secs[i]; in linker_append_elf_relos()
2210 src_linked_sec = &obj->secs[src_sec->shdr->sh_info]; in linker_append_elf_relos()
2251 dst_sym_idx = obj->sym_map[src_sym_idx]; in linker_append_elf_relos()
2257 struct src_sec *sec = &obj->secs[src_sym->st_shndx]; in linker_append_elf_relos()
2287 static Elf64_Sym *find_sym_by_name(struct src_obj *obj, size_t sec_idx, in find_sym_by_name() argument
2290 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx]; in find_sym_by_name()
2302 name = elf_strptr(obj->elf, str_sec_idx, sym->st_name); in find_sym_by_name()
2315 static int linker_fixup_btf(struct src_obj *obj) in linker_fixup_btf() argument
2321 if (!obj->btf) in linker_fixup_btf()
2324 n = btf__type_cnt(obj->btf); in linker_fixup_btf()
2329 t = btf_type_by_id(obj->btf, i); in linker_fixup_btf()
2333 sec_name = btf__str_by_offset(obj->btf, t->name_off); in linker_fixup_btf()
2334 sec = find_src_sec_by_name(obj, sec_name); in linker_fixup_btf()
2367 sec = add_src_sec(obj, sec_name); in linker_fixup_btf()
2381 const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type); in linker_fixup_btf()
2390 var_name = btf__str_by_offset(obj->btf, vt->name_off); in linker_fixup_btf()
2397 sym = find_sym_by_name(obj, sec->sec_idx, STT_OBJECT, var_name); in linker_fixup_btf()
2410 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj) in linker_append_btf() argument
2416 if (!obj->btf) in linker_append_btf()
2420 n = btf__type_cnt(obj->btf); in linker_append_btf()
2422 obj->btf_type_map = calloc(n + 1, sizeof(int)); in linker_append_btf()
2423 if (!obj->btf_type_map) in linker_append_btf()
2429 t = btf__type_by_id(obj->btf, i); in linker_append_btf()
2437 name = btf__str_by_offset(obj->btf, t->name_off); in linker_append_btf()
2460 obj->btf_type_map[i] = glob_sym->btf_id; in linker_append_btf()
2465 id = btf__add_type(linker->btf, obj->btf, t); in linker_append_btf()
2467 pr_warn("failed to append BTF type #%d from file '%s'\n", i, obj->filename); in linker_append_btf()
2471 obj->btf_type_map[i] = id; in linker_append_btf()
2492 int new_id = obj->btf_type_map[*type_id]; in linker_append_btf()
2501 *type_id = obj->btf_type_map[*type_id]; in linker_append_btf()
2515 glob_sym->underlying_btf_id = obj->btf_type_map[-glob_sym->underlying_btf_id]; in linker_append_btf()
2522 for (i = 1; i < obj->sec_cnt; i++) { in linker_append_btf()
2528 src_sec = &obj->secs[i]; in linker_append_btf()
2543 t = btf__type_by_id(obj->btf, src_sec->sec_type_id); in linker_append_btf()
2548 int new_id = obj->btf_type_map[src_var->type]; in linker_append_btf()
2599 dst_var->type = obj->btf_type_map[src_var->type]; in linker_append_btf()
2628 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj) in linker_append_btf_ext() argument
2636 if (!obj->btf_ext) in linker_append_btf_ext()
2639 rec_sz = obj->btf_ext->func_info.rec_size; in linker_append_btf_ext()
2640 for_each_btf_ext_sec(&obj->btf_ext->func_info, ext_sec) { in linker_append_btf_ext()
2643 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off); in linker_append_btf_ext()
2644 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_btf_ext()
2658 for_each_btf_ext_rec(&obj->btf_ext->func_info, ext_sec, i, src_rec) { in linker_append_btf_ext()
2664 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id]; in linker_append_btf_ext()
2668 rec_sz = obj->btf_ext->line_info.rec_size; in linker_append_btf_ext()
2669 for_each_btf_ext_sec(&obj->btf_ext->line_info, ext_sec) { in linker_append_btf_ext()
2672 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off); in linker_append_btf_ext()
2673 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_btf_ext()
2687 for_each_btf_ext_rec(&obj->btf_ext->line_info, ext_sec, i, src_rec) { in linker_append_btf_ext()
2694 s = btf__str_by_offset(obj->btf, src_rec->file_name_off); in linker_append_btf_ext()
2700 s = btf__str_by_offset(obj->btf, src_rec->line_off); in linker_append_btf_ext()
2710 rec_sz = obj->btf_ext->core_relo_info.rec_size; in linker_append_btf_ext()
2711 for_each_btf_ext_sec(&obj->btf_ext->core_relo_info, ext_sec) { in linker_append_btf_ext()
2714 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off); in linker_append_btf_ext()
2715 src_sec = find_src_sec_by_name(obj, sec_name); in linker_append_btf_ext()
2729 for_each_btf_ext_rec(&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) { in linker_append_btf_ext()
2735 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id]; in linker_append_btf_ext()
2737 s = btf__str_by_offset(obj->btf, src_rec->access_str_off); in linker_append_btf_ext()