Lines Matching defs:obj

162 				struct src_obj *obj);
163 static int linker_sanity_check_elf(struct src_obj *obj);
164 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
165 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec);
166 static int linker_sanity_check_btf(struct src_obj *obj);
167 static int linker_sanity_check_btf_ext(struct src_obj *obj);
168 static int linker_fixup_btf(struct src_obj *obj);
169 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj);
170 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj);
171 static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
173 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj);
174 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj);
175 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj);
443 struct src_obj obj = {};
452 err = err ?: linker_load_obj_file(linker, filename, opts, &obj);
453 err = err ?: linker_append_sec_data(linker, &obj);
454 err = err ?: linker_append_elf_syms(linker, &obj);
455 err = err ?: linker_append_elf_relos(linker, &obj);
456 err = err ?: linker_append_btf(linker, &obj);
457 err = err ?: linker_append_btf_ext(linker, &obj);
460 free(obj.btf_type_map);
461 btf__free(obj.btf);
462 btf_ext__free(obj.btf_ext);
463 free(obj.secs);
464 free(obj.sym_map);
465 if (obj.elf)
466 elf_end(obj.elf);
467 if (obj.fd >= 0)
468 close(obj.fd);
516 static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name)
518 struct src_sec *secs = obj->secs, *sec;
519 size_t new_cnt = obj->sec_cnt ? obj->sec_cnt + 1 : 2;
526 memset(secs + obj->sec_cnt, 0, (new_cnt - obj->sec_cnt) * sizeof(*secs));
528 obj->secs = secs;
529 obj->sec_cnt = new_cnt;
531 sec = &obj->secs[new_cnt - 1];
540 struct src_obj *obj)
558 obj->filename = filename;
560 obj->fd = open(filename, O_RDONLY | O_CLOEXEC);
561 if (obj->fd < 0) {
566 obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL);
567 if (!obj->elf) {
574 ehdr = elf64_getehdr(obj->elf);
593 if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) {
600 while ((scn = elf_nextscn(obj->elf, scn)) != NULL) {
612 sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name);
628 sec = add_src_sec(obj, sec_name);
644 if (obj->symtab_sec_idx) {
649 obj->symtab_sec_idx = sec_idx;
656 obj->btf = btf__new(data->d_buf, shdr->sh_size);
657 err = libbpf_get_error(obj->btf);
666 obj->btf_ext = btf_ext__new(data->d_buf, shdr->sh_size);
667 err = libbpf_get_error(obj->btf_ext);
692 err = err ?: linker_sanity_check_elf(obj);
693 err = err ?: linker_sanity_check_btf(obj);
694 err = err ?: linker_sanity_check_btf_ext(obj);
695 err = err ?: linker_fixup_btf(obj);
700 static int linker_sanity_check_elf(struct src_obj *obj)
705 if (!obj->symtab_sec_idx) {
706 pr_warn("ELF is missing SYMTAB section in %s\n", obj->filename);
709 if (!obj->shstrs_sec_idx) {
710 pr_warn("ELF is missing section headers STRTAB section in %s\n", obj->filename);
714 for (i = 1; i < obj->sec_cnt; i++) {
715 sec = &obj->secs[i];
718 pr_warn("ELF section #%zu has empty name in %s\n", sec->sec_idx, obj->filename);
732 err = linker_sanity_check_elf_symtab(obj, sec);
747 err = linker_sanity_check_elf_relos(obj, sec);
755 sec->sec_idx, sec->sec_name, (size_t)sec->shdr->sh_type, obj->filename);
763 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec)
774 if (!sec->shdr->sh_link || sec->shdr->sh_link >= obj->sec_cnt) {
776 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
779 link_sec = &obj->secs[sec->shdr->sh_link];
782 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
797 pr_warn("ELF sym #0 is invalid in %s\n", obj->filename);
816 i, obj->filename);
822 if (sym->st_shndx < SHN_LORESERVE && sym->st_shndx >= obj->sec_cnt) {
824 i, sec->sec_idx, (size_t)sym->st_shndx, obj->filename);
837 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec)
849 if (sec->shdr->sh_link != obj->symtab_sec_idx) {
851 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
856 if (!sec->shdr->sh_info || sec->shdr->sh_info >= obj->sec_cnt) {
858 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
861 link_sec = &obj->secs[sec->shdr->sh_info];
867 sec->sec_idx, obj->filename);
878 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
885 sym_sec = &obj->secs[obj->symtab_sec_idx];
893 i, sec->sec_idx, sym_type, obj->filename);
899 i, sec->sec_idx, sym_idx, obj->filename);
906 i, sec->sec_idx, sym_idx, obj->filename);
938 static int linker_sanity_check_btf(struct src_obj *obj)
943 if (!obj->btf)
946 n = btf__type_cnt(obj->btf);
948 t = btf_type_by_id(obj->btf, i);
950 err = err ?: btf_type_visit_type_ids(t, check_btf_type_id, obj->btf);
951 err = err ?: btf_type_visit_str_offs(t, check_btf_str_off, obj->btf);
959 static int linker_sanity_check_btf_ext(struct src_obj *obj)
963 if (!obj->btf_ext)
967 if (!obj->btf)
970 err = err ?: btf_ext_visit_type_ids(obj->btf_ext, check_btf_type_id, obj->btf);
971 err = err ?: btf_ext_visit_str_offs(obj->btf_ext, check_btf_str_off, obj->btf);
1169 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj)
1173 for (i = 1; i < obj->sec_cnt; i++) {
1177 src_sec = &obj->secs[i];
1221 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj)
1223 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
1229 obj->sym_map = calloc(n + 1, sizeof(*obj->sym_map));
1230 if (!obj->sym_map)
1240 sym_name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
1242 pr_warn("can't fetch symbol name for symbol #%d in '%s'\n", i, obj->filename);
1246 err = linker_append_elf_sym(linker, obj, sym, sym_name, i);
1582 struct src_obj *obj, Elf64_Sym *sym, int btf_id)
1589 t = btf__type_by_id(obj->btf, btf_id);
1594 t = skip_mods_and_typedefs(obj->btf, t->type, NULL);
1596 err = parse_btf_map_def(sym_name, obj->btf, t, true /*strict*/, &src_def, &src_inner_def);
1617 obj->btf, &src_def, &src_inner_def);
1622 struct src_obj *obj, Elf64_Sym *sym, size_t sym_idx, int btf_id)
1634 src_t = btf__type_by_id(obj->btf, btf_id);
1643 return glob_map_defs_match(sym_name, linker, glob_sym, obj, sym, btf_id);
1646 linker->btf, glob_sym->btf_id, obj->btf, btf_id))
1658 static int find_glob_sym_btf(struct src_obj *obj, Elf64_Sym *sym, const char *sym_name,
1666 if (!obj->btf) {
1667 pr_warn("failed to find BTF info for object '%s'\n", obj->filename);
1671 n = btf__type_cnt(obj->btf);
1673 t = btf__type_by_id(obj->btf, i);
1679 name = btf__str_by_offset(obj->btf, t->name_off);
1693 t = btf__type_by_id(obj->btf, vi->type);
1694 name = btf__str_by_offset(obj->btf, t->name_off);
1727 static struct src_sec *find_src_sec_by_name(struct src_obj *obj, const char *sec_name)
1732 for (i = 1; i < obj->sec_cnt; i++) {
1733 sec = &obj->secs[i];
1811 static int linker_append_elf_sym(struct bpf_linker *linker, struct src_obj *obj,
1829 if (!obj->btf) {
1834 src_sec = &obj->secs[sym->st_shndx];
1841 obj->sym_map[src_sym_idx] = dst_sec->sec_sym_idx;
1850 err = find_glob_sym_btf(obj, sym, sym_name, &btf_sec_id, &btf_id);
1858 t = btf__type_by_id(obj->btf, btf_sec_id);
1859 sec_name = btf__str_by_offset(obj->btf, t->name_off);
1868 src_sec = find_src_sec_by_name(obj, sec_name);
1883 obj->sym_map[src_sym_idx] = glob_sym->sym_idx;
1892 src_sym_idx, sym_name, obj->filename);
1896 if (!glob_syms_match(sym_name, linker, glob_sym, obj, sym, src_sym_idx, btf_id))
1953 obj->btf, btf_id))
1959 obj->sym_map[src_sym_idx] = glob_sym->sym_idx;
1979 obj->sym_map[src_sym_idx] = dst_sym_idx;
2009 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj)
2011 struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx];
2014 for (i = 1; i < obj->sec_cnt; i++) {
2020 src_sec = &obj->secs[i];
2025 src_linked_sec = &obj->secs[src_sec->shdr->sh_info];
2066 dst_sym_idx = obj->sym_map[src_sym_idx];
2072 struct src_sec *sec = &obj->secs[src_sym->st_shndx];
2102 static Elf64_Sym *find_sym_by_name(struct src_obj *obj, size_t sec_idx,
2105 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
2117 name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
2130 static int linker_fixup_btf(struct src_obj *obj)
2136 if (!obj->btf)
2139 n = btf__type_cnt(obj->btf);
2144 t = btf_type_by_id(obj->btf, i);
2148 sec_name = btf__str_by_offset(obj->btf, t->name_off);
2149 sec = find_src_sec_by_name(obj, sec_name);
2182 sec = add_src_sec(obj, sec_name);
2196 const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type);
2197 const char *var_name = btf__str_by_offset(obj->btf, vt->name_off);
2205 sym = find_sym_by_name(obj, sec->sec_idx, STT_OBJECT, var_name);
2234 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
2240 if (!obj->btf)
2244 n = btf__type_cnt(obj->btf);
2246 obj->btf_type_map = calloc(n + 1, sizeof(int));
2247 if (!obj->btf_type_map)
2253 t = btf__type_by_id(obj->btf, i);
2261 name = btf__str_by_offset(obj->btf, t->name_off);
2284 obj->btf_type_map[i] = glob_sym->btf_id;
2289 id = btf__add_type(linker->btf, obj->btf, t);
2291 pr_warn("failed to append BTF type #%d from file '%s'\n", i, obj->filename);
2295 obj->btf_type_map[i] = id;
2309 if (btf_type_visit_type_ids(dst_t, remap_type_id, obj->btf_type_map))
2323 glob_sym->underlying_btf_id = obj->btf_type_map[-glob_sym->underlying_btf_id];
2330 for (i = 1; i < obj->sec_cnt; i++) {
2336 src_sec = &obj->secs[i];
2351 t = btf__type_by_id(obj->btf, src_sec->sec_type_id);
2356 int new_id = obj->btf_type_map[src_var->type];
2403 dst_var->type = obj->btf_type_map[src_var->type];
2432 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj)
2440 if (!obj->btf_ext)
2443 rec_sz = obj->btf_ext->func_info.rec_size;
2444 for_each_btf_ext_sec(&obj->btf_ext->func_info, ext_sec) {
2447 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
2448 src_sec = find_src_sec_by_name(obj, sec_name);
2462 for_each_btf_ext_rec(&obj->btf_ext->func_info, ext_sec, i, src_rec) {
2468 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
2472 rec_sz = obj->btf_ext->line_info.rec_size;
2473 for_each_btf_ext_sec(&obj->btf_ext->line_info, ext_sec) {
2476 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
2477 src_sec = find_src_sec_by_name(obj, sec_name);
2491 for_each_btf_ext_rec(&obj->btf_ext->line_info, ext_sec, i, src_rec) {
2498 s = btf__str_by_offset(obj->btf, src_rec->file_name_off);
2504 s = btf__str_by_offset(obj->btf, src_rec->line_off);
2514 rec_sz = obj->btf_ext->core_relo_info.rec_size;
2515 for_each_btf_ext_sec(&obj->btf_ext->core_relo_info, ext_sec) {
2518 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
2519 src_sec = find_src_sec_by_name(obj, sec_name);
2533 for_each_btf_ext_rec(&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) {
2539 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
2541 s = btf__str_by_offset(obj->btf, src_rec->access_str_off);