1 // SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
5 * Copyright (c) 2021 Facebook
14 #include <linux/err.h>
15 #include <linux/btf.h>
22 #include "libbpf_internal.h"
27 /* positional (not necessarily ELF) index in an array of sections */
29 /* positional (not necessarily ELF) index of a matching section in a final object file */
31 /* section data offset in a matching output section */
33 /* whether section is omitted from the final ELF file */
35 /* whether section is an ephemeral section, not mapped to an ELF section */
44 /* corresponding BTF DATASEC type ID */
52 /* Section header strings section index */
53 size_t shstrs_sec_idx;
54 /* SYMTAB section index */
55 size_t symtab_sec_idx;
58 struct btf_ext *btf_ext;
60 /* List of sections (including ephemeral). Slot zero is unused. */
64 /* mapping of symbol indices from src to dst ELF */
66 /* mapping from the src BTF type IDs to dst ones */
70 /* single .BTF.ext data section */
71 struct btf_ext_sec_data {
79 /* positional (not necessarily ELF) index in an array of sections */
88 /* final output section size */
90 /* final output contents of the section */
93 /* corresponding STT_SECTION symbol index in SYMTAB */
96 /* section's DATASEC variable info, emitted on BTF finalization */
99 struct btf_var_secinfo *sec_vars;
101 /* section's .BTF.ext data */
102 struct btf_ext_sec_data func_info;
103 struct btf_ext_sec_data line_info;
104 struct btf_ext_sec_data core_relo_info;
113 /* Output sections metadata */
114 struct dst_sec *secs;
117 struct strset *strtab_strs; /* STRTAB unique strings */
118 size_t strtab_sec_idx; /* STRTAB section index */
119 size_t symtab_sec_idx; /* SYMTAB section index */
122 struct btf_ext *btf_ext;
125 #define pr_warn_elf(fmt, ...) \
126 libbpf_print(LIBBPF_WARN, "libbpf: " fmt ": %s\n", ##__VA_ARGS__, elf_errmsg(-1))
128 static int init_output_elf(struct bpf_linker *linker, const char *file);
130 static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, struct src_obj *obj);
131 static int linker_sanity_check_elf(struct src_obj *obj);
132 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec);
133 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec);
134 static int linker_sanity_check_btf(struct src_obj *obj);
135 static int linker_sanity_check_btf_ext(struct src_obj *obj);
136 static int linker_fixup_btf(struct src_obj *obj);
137 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj);
138 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj);
139 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj);
140 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj);
141 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj);
143 static int finalize_btf(struct bpf_linker *linker);
144 static int finalize_btf_ext(struct bpf_linker *linker);
146 void bpf_linker__free(struct bpf_linker *linker)
153 free(linker->filename);
156 elf_end(linker->elf);
161 strset__free(linker->strtab_strs);
163 btf__free(linker->btf);
164 btf_ext__free(linker->btf_ext);
166 for (i = 1; i < linker->sec_cnt; i++) {
167 struct dst_sec *sec = &linker->secs[i];
173 free(sec->func_info.recs);
174 free(sec->line_info.recs);
175 free(sec->core_relo_info.recs);
182 struct bpf_linker *bpf_linker__new(const char *filename, struct bpf_linker_opts *opts)
184 struct bpf_linker *linker;
187 if (!OPTS_VALID(opts, bpf_linker_opts))
190 if (elf_version(EV_CURRENT) == EV_NONE) {
191 pr_warn_elf("libelf initialization failed");
195 linker = calloc(1, sizeof(*linker));
201 err = init_output_elf(linker, filename);
208 bpf_linker__free(linker);
212 static struct dst_sec *add_dst_sec(struct bpf_linker *linker, const char *sec_name)
214 struct dst_sec *secs = linker->secs, *sec;
215 size_t new_cnt = linker->sec_cnt ? linker->sec_cnt + 1 : 2;
217 secs = libbpf_reallocarray(secs, new_cnt, sizeof(*secs));
221 /* zero out newly allocated memory */
222 memset(secs + linker->sec_cnt, 0, (new_cnt - linker->sec_cnt) * sizeof(*secs));
225 linker->sec_cnt = new_cnt;
227 sec = &linker->secs[new_cnt - 1];
228 sec->id = new_cnt - 1;
229 sec->sec_name = strdup(sec_name);
236 static Elf64_Sym *add_new_sym(struct bpf_linker *linker, size_t *sym_idx)
238 struct dst_sec *symtab = &linker->secs[linker->symtab_sec_idx];
239 Elf64_Sym *syms, *sym;
240 size_t sym_cnt = symtab->sec_sz / sizeof(*sym);
242 syms = libbpf_reallocarray(symtab->raw_data, sym_cnt + 1, sizeof(*sym));
246 sym = &syms[sym_cnt];
247 memset(sym, 0, sizeof(*sym));
249 symtab->raw_data = syms;
250 symtab->sec_sz += sizeof(*sym);
251 symtab->shdr->sh_size += sizeof(*sym);
252 symtab->data->d_size += sizeof(*sym);
260 static int init_output_elf(struct bpf_linker *linker, const char *file)
266 linker->filename = strdup(file);
267 if (!linker->filename)
270 linker->fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644);
271 if (linker->fd < 0) {
273 pr_warn("failed to create '%s': %d\n", file, err);
277 linker->elf = elf_begin(linker->fd, ELF_C_WRITE, NULL);
279 pr_warn_elf("failed to create ELF object");
284 linker->elf_hdr = elf64_newehdr(linker->elf);
285 if (!linker->elf_hdr) {
286 pr_warn_elf("failed to create ELF header");
290 linker->elf_hdr->e_machine = EM_BPF;
291 linker->elf_hdr->e_type = ET_REL;
292 #if __BYTE_ORDER == __LITTLE_ENDIAN
293 linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2LSB;
294 #elif __BYTE_ORDER == __BIG_ENDIAN
295 linker->elf_hdr->e_ident[EI_DATA] = ELFDATA2MSB;
297 #error "Unknown __BYTE_ORDER"
301 /* initialize strset with an empty string to conform to ELF */
302 linker->strtab_strs = strset__new(INT_MAX, "", sizeof(""));
303 if (libbpf_get_error(linker->strtab_strs))
304 return libbpf_get_error(linker->strtab_strs);
306 sec = add_dst_sec(linker, ".strtab");
310 sec->scn = elf_newscn(linker->elf);
312 pr_warn_elf("failed to create STRTAB section");
316 sec->shdr = elf64_getshdr(sec->scn);
320 sec->data = elf_newdata(sec->scn);
322 pr_warn_elf("failed to create STRTAB data");
326 str_off = strset__add_str(linker->strtab_strs, sec->sec_name);
330 sec->sec_idx = elf_ndxscn(sec->scn);
331 linker->elf_hdr->e_shstrndx = sec->sec_idx;
332 linker->strtab_sec_idx = sec->sec_idx;
334 sec->shdr->sh_name = str_off;
335 sec->shdr->sh_type = SHT_STRTAB;
336 sec->shdr->sh_flags = SHF_STRINGS;
337 sec->shdr->sh_offset = 0;
338 sec->shdr->sh_link = 0;
339 sec->shdr->sh_info = 0;
340 sec->shdr->sh_addralign = 1;
341 sec->shdr->sh_size = sec->sec_sz = 0;
342 sec->shdr->sh_entsize = 0;
345 sec = add_dst_sec(linker, ".symtab");
349 sec->scn = elf_newscn(linker->elf);
351 pr_warn_elf("failed to create SYMTAB section");
355 sec->shdr = elf64_getshdr(sec->scn);
359 sec->data = elf_newdata(sec->scn);
361 pr_warn_elf("failed to create SYMTAB data");
365 str_off = strset__add_str(linker->strtab_strs, sec->sec_name);
369 sec->sec_idx = elf_ndxscn(sec->scn);
370 linker->symtab_sec_idx = sec->sec_idx;
372 sec->shdr->sh_name = str_off;
373 sec->shdr->sh_type = SHT_SYMTAB;
374 sec->shdr->sh_flags = 0;
375 sec->shdr->sh_offset = 0;
376 sec->shdr->sh_link = linker->strtab_sec_idx;
377 /* sh_info should be one greater than the index of the last local
378 * symbol (i.e., binding is STB_LOCAL). But why and who cares?
380 sec->shdr->sh_info = 0;
381 sec->shdr->sh_addralign = 8;
382 sec->shdr->sh_entsize = sizeof(Elf64_Sym);
385 linker->btf = btf__new_empty();
386 err = libbpf_get_error(linker->btf);
390 /* add the special all-zero symbol */
391 init_sym = add_new_sym(linker, NULL);
395 init_sym->st_name = 0;
396 init_sym->st_info = 0;
397 init_sym->st_other = 0;
398 init_sym->st_shndx = SHN_UNDEF;
399 init_sym->st_value = 0;
400 init_sym->st_size = 0;
405 int bpf_linker__add_file(struct bpf_linker *linker, const char *filename)
407 struct src_obj obj = {};
413 err = err ?: linker_load_obj_file(linker, filename, &obj);
414 err = err ?: linker_append_sec_data(linker, &obj);
415 err = err ?: linker_append_elf_syms(linker, &obj);
416 err = err ?: linker_append_elf_relos(linker, &obj);
417 err = err ?: linker_append_btf(linker, &obj);
418 err = err ?: linker_append_btf_ext(linker, &obj);
420 /* free up src_obj resources */
421 free(obj.btf_type_map);
423 btf_ext__free(obj.btf_ext);
434 static bool is_dwarf_sec_name(const char *name)
436 /* approximation, but the actual list is too long */
437 return strncmp(name, ".debug_", sizeof(".debug_") - 1) == 0;
440 static bool is_ignored_sec(struct src_sec *sec)
442 Elf64_Shdr *shdr = sec->shdr;
443 const char *name = sec->sec_name;
445 /* no special handling of .strtab */
446 if (shdr->sh_type == SHT_STRTAB)
449 /* ignore .llvm_addrsig section as well */
450 if (shdr->sh_type == SHT_LLVM_ADDRSIG)
453 /* no subprograms will lead to an empty .text section, ignore it */
454 if (shdr->sh_type == SHT_PROGBITS && shdr->sh_size == 0 &&
455 strcmp(sec->sec_name, ".text") == 0)
459 if (is_dwarf_sec_name(sec->sec_name))
462 if (strncmp(name, ".rel", sizeof(".rel") - 1) == 0) {
463 name += sizeof(".rel") - 1;
464 /* DWARF section relocations */
465 if (is_dwarf_sec_name(name))
468 /* .BTF and .BTF.ext don't need relocations */
469 if (strcmp(name, BTF_ELF_SEC) == 0 ||
470 strcmp(name, BTF_EXT_ELF_SEC) == 0)
477 static struct src_sec *add_src_sec(struct src_obj *obj, const char *sec_name)
479 struct src_sec *secs = obj->secs, *sec;
480 size_t new_cnt = obj->sec_cnt ? obj->sec_cnt + 1 : 2;
482 secs = libbpf_reallocarray(secs, new_cnt, sizeof(*secs));
486 /* zero out newly allocated memory */
487 memset(secs + obj->sec_cnt, 0, (new_cnt - obj->sec_cnt) * sizeof(*secs));
490 obj->sec_cnt = new_cnt;
492 sec = &obj->secs[new_cnt - 1];
493 sec->id = new_cnt - 1;
494 sec->sec_name = sec_name;
499 static int linker_load_obj_file(struct bpf_linker *linker, const char *filename, struct src_obj *obj)
501 #if __BYTE_ORDER == __LITTLE_ENDIAN
502 const int host_endianness = ELFDATA2LSB;
503 #elif __BYTE_ORDER == __BIG_ENDIAN
504 const int host_endianness = ELFDATA2MSB;
506 #error "Unknown __BYTE_ORDER"
515 pr_debug("linker: adding object file '%s'...\n", filename);
517 obj->filename = filename;
519 obj->fd = open(filename, O_RDONLY);
522 pr_warn("failed to open file '%s': %d\n", filename, err);
525 obj->elf = elf_begin(obj->fd, ELF_C_READ_MMAP, NULL);
528 pr_warn_elf("failed to parse ELF file '%s'", filename);
532 /* Sanity check ELF file high-level properties */
533 ehdr = elf64_getehdr(obj->elf);
536 pr_warn_elf("failed to get ELF header for %s", filename);
539 if (ehdr->e_ident[EI_DATA] != host_endianness) {
541 pr_warn_elf("unsupported byte order of ELF file %s", filename);
544 if (ehdr->e_type != ET_REL
545 || ehdr->e_machine != EM_BPF
546 || ehdr->e_ident[EI_CLASS] != ELFCLASS64) {
548 pr_warn_elf("unsupported kind of ELF file %s", filename);
552 if (elf_getshdrstrndx(obj->elf, &obj->shstrs_sec_idx)) {
554 pr_warn_elf("failed to get SHSTRTAB section index for %s", filename);
559 while ((scn = elf_nextscn(obj->elf, scn)) != NULL) {
560 size_t sec_idx = elf_ndxscn(scn);
561 const char *sec_name;
563 shdr = elf64_getshdr(scn);
566 pr_warn_elf("failed to get section #%zu header for %s",
571 sec_name = elf_strptr(obj->elf, obj->shstrs_sec_idx, shdr->sh_name);
574 pr_warn_elf("failed to get section #%zu name for %s",
579 data = elf_getdata(scn, 0);
582 pr_warn_elf("failed to get section #%zu (%s) data from %s",
583 sec_idx, sec_name, filename);
587 sec = add_src_sec(obj, sec_name);
594 sec->sec_idx = elf_ndxscn(scn);
596 if (is_ignored_sec(sec)) {
601 switch (shdr->sh_type) {
603 if (obj->symtab_sec_idx) {
605 pr_warn("multiple SYMTAB sections found, not supported\n");
608 obj->symtab_sec_idx = sec_idx;
611 /* we'll construct our own string table */
614 if (strcmp(sec_name, BTF_ELF_SEC) == 0) {
615 obj->btf = btf__new(data->d_buf, shdr->sh_size);
616 err = libbpf_get_error(obj->btf);
618 pr_warn("failed to parse .BTF from %s: %d\n", filename, err);
624 if (strcmp(sec_name, BTF_EXT_ELF_SEC) == 0) {
625 obj->btf_ext = btf_ext__new(data->d_buf, shdr->sh_size);
626 err = libbpf_get_error(obj->btf_ext);
628 pr_warn("failed to parse .BTF.ext from '%s': %d\n", filename, err);
644 pr_warn("unrecognized section #%zu (%s) in %s\n",
645 sec_idx, sec_name, filename);
651 err = err ?: linker_sanity_check_elf(obj);
652 err = err ?: linker_sanity_check_btf(obj);
653 err = err ?: linker_sanity_check_btf_ext(obj);
654 err = err ?: linker_fixup_btf(obj);
659 static bool is_pow_of_2(size_t x)
661 return x && (x & (x - 1)) == 0;
664 static int linker_sanity_check_elf(struct src_obj *obj)
669 if (!obj->symtab_sec_idx) {
670 pr_warn("ELF is missing SYMTAB section in %s\n", obj->filename);
673 if (!obj->shstrs_sec_idx) {
674 pr_warn("ELF is missing section headers STRTAB section in %s\n", obj->filename);
678 for (i = 1; i < obj->sec_cnt; i++) {
681 if (sec->sec_name[0] == '\0') {
682 pr_warn("ELF section #%zu has empty name in %s\n", sec->sec_idx, obj->filename);
686 if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
688 if (sec->shdr->sh_addralign != sec->data->d_align)
691 if (sec->shdr->sh_size != sec->data->d_size)
694 switch (sec->shdr->sh_type) {
696 err = linker_sanity_check_elf_symtab(obj, sec);
703 if (sec->shdr->sh_flags & SHF_EXECINSTR) {
704 if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
711 err = linker_sanity_check_elf_relos(obj, sec);
715 case SHT_LLVM_ADDRSIG:
718 pr_warn("ELF section #%zu (%s) has unrecognized type %zu in %s\n",
719 sec->sec_idx, sec->sec_name, (size_t)sec->shdr->sh_type, obj->filename);
727 static int linker_sanity_check_elf_symtab(struct src_obj *obj, struct src_sec *sec)
729 struct src_sec *link_sec;
733 if (sec->shdr->sh_entsize != sizeof(Elf64_Sym))
735 if (sec->shdr->sh_size % sec->shdr->sh_entsize != 0)
738 if (!sec->shdr->sh_link || sec->shdr->sh_link >= obj->sec_cnt) {
739 pr_warn("ELF SYMTAB section #%zu points to missing STRTAB section #%zu in %s\n",
740 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
743 link_sec = &obj->secs[sec->shdr->sh_link];
744 if (link_sec->shdr->sh_type != SHT_STRTAB) {
745 pr_warn("ELF SYMTAB section #%zu points to invalid STRTAB section #%zu in %s\n",
746 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
750 n = sec->shdr->sh_size / sec->shdr->sh_entsize;
751 sym = sec->data->d_buf;
752 for (i = 0; i < n; i++, sym++) {
754 && sym->st_shndx < SHN_LORESERVE
755 && sym->st_shndx >= obj->sec_cnt) {
756 pr_warn("ELF sym #%d in section #%zu points to missing section #%zu in %s\n",
757 i, sec->sec_idx, (size_t)sym->st_shndx, obj->filename);
760 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION) {
761 if (sym->st_value != 0)
770 static int linker_sanity_check_elf_relos(struct src_obj *obj, struct src_sec *sec)
772 struct src_sec *link_sec, *sym_sec;
776 if (sec->shdr->sh_entsize != sizeof(Elf64_Rel))
778 if (sec->shdr->sh_size % sec->shdr->sh_entsize != 0)
781 /* SHT_REL's sh_link should point to SYMTAB */
782 if (sec->shdr->sh_link != obj->symtab_sec_idx) {
783 pr_warn("ELF relo section #%zu points to invalid SYMTAB section #%zu in %s\n",
784 sec->sec_idx, (size_t)sec->shdr->sh_link, obj->filename);
788 /* SHT_REL's sh_info points to relocated section */
789 if (!sec->shdr->sh_info || sec->shdr->sh_info >= obj->sec_cnt) {
790 pr_warn("ELF relo section #%zu points to missing section #%zu in %s\n",
791 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
794 link_sec = &obj->secs[sec->shdr->sh_info];
796 /* .rel<secname> -> <secname> pattern is followed */
797 if (strncmp(sec->sec_name, ".rel", sizeof(".rel") - 1) != 0
798 || strcmp(sec->sec_name + sizeof(".rel") - 1, link_sec->sec_name) != 0) {
799 pr_warn("ELF relo section #%zu name has invalid name in %s\n",
800 sec->sec_idx, obj->filename);
804 /* don't further validate relocations for ignored sections */
805 if (link_sec->skipped)
808 /* relocatable section is data or instructions */
809 if (link_sec->shdr->sh_type != SHT_PROGBITS && link_sec->shdr->sh_type != SHT_NOBITS) {
810 pr_warn("ELF relo section #%zu points to invalid section #%zu in %s\n",
811 sec->sec_idx, (size_t)sec->shdr->sh_info, obj->filename);
815 /* check sanity of each relocation */
816 n = sec->shdr->sh_size / sec->shdr->sh_entsize;
817 relo = sec->data->d_buf;
818 sym_sec = &obj->secs[obj->symtab_sec_idx];
819 for (i = 0; i < n; i++, relo++) {
820 size_t sym_idx = ELF64_R_SYM(relo->r_info);
821 size_t sym_type = ELF64_R_TYPE(relo->r_info);
823 if (sym_type != R_BPF_64_64 && sym_type != R_BPF_64_32) {
824 pr_warn("ELF relo #%d in section #%zu has unexpected type %zu in %s\n",
825 i, sec->sec_idx, sym_type, obj->filename);
829 if (!sym_idx || sym_idx * sizeof(Elf64_Sym) >= sym_sec->shdr->sh_size) {
830 pr_warn("ELF relo #%d in section #%zu points to invalid symbol #%zu in %s\n",
831 i, sec->sec_idx, sym_idx, obj->filename);
835 if (link_sec->shdr->sh_flags & SHF_EXECINSTR) {
836 if (relo->r_offset % sizeof(struct bpf_insn) != 0) {
837 pr_warn("ELF relo #%d in section #%zu points to missing symbol #%zu in %s\n",
838 i, sec->sec_idx, sym_idx, obj->filename);
847 static int check_btf_type_id(__u32 *type_id, void *ctx)
849 struct btf *btf = ctx;
851 if (*type_id > btf__get_nr_types(btf))
857 static int check_btf_str_off(__u32 *str_off, void *ctx)
859 struct btf *btf = ctx;
862 s = btf__str_by_offset(btf, *str_off);
870 static int linker_sanity_check_btf(struct src_obj *obj)
878 n = btf__get_nr_types(obj->btf);
879 for (i = 1; i <= n; i++) {
880 t = btf_type_by_id(obj->btf, i);
882 err = err ?: btf_type_visit_type_ids(t, check_btf_type_id, obj->btf);
883 err = err ?: btf_type_visit_str_offs(t, check_btf_str_off, obj->btf);
891 static int linker_sanity_check_btf_ext(struct src_obj *obj)
898 /* can't use .BTF.ext without .BTF */
902 err = err ?: btf_ext_visit_type_ids(obj->btf_ext, check_btf_type_id, obj->btf);
903 err = err ?: btf_ext_visit_str_offs(obj->btf_ext, check_btf_str_off, obj->btf);
910 static int init_sec(struct bpf_linker *linker, struct dst_sec *dst_sec, struct src_sec *src_sec)
918 dst_sec->sec_idx = 0;
920 /* ephemeral sections are just thin section shells lacking most parts */
921 if (src_sec->ephemeral)
924 scn = elf_newscn(linker->elf);
927 data = elf_newdata(scn);
930 shdr = elf64_getshdr(scn);
935 dst_sec->shdr = shdr;
936 dst_sec->data = data;
937 dst_sec->sec_idx = elf_ndxscn(scn);
939 name_off = strset__add_str(linker->strtab_strs, src_sec->sec_name);
943 shdr->sh_name = name_off;
944 shdr->sh_type = src_sec->shdr->sh_type;
945 shdr->sh_flags = src_sec->shdr->sh_flags;
947 /* sh_link and sh_info have different meaning for different types of
948 * sections, so we leave it up to the caller code to fill them in, if
953 shdr->sh_addralign = src_sec->shdr->sh_addralign;
954 shdr->sh_entsize = src_sec->shdr->sh_entsize;
956 data->d_type = src_sec->data->d_type;
959 data->d_align = src_sec->data->d_align;
965 static struct dst_sec *find_dst_sec_by_name(struct bpf_linker *linker, const char *sec_name)
970 for (i = 1; i < linker->sec_cnt; i++) {
971 sec = &linker->secs[i];
973 if (strcmp(sec->sec_name, sec_name) == 0)
980 static bool secs_match(struct dst_sec *dst, struct src_sec *src)
982 if (dst->shdr->sh_type != src->shdr->sh_type) {
983 pr_warn("sec %s types mismatch\n", dst->sec_name);
986 if (dst->shdr->sh_flags != src->shdr->sh_flags) {
987 pr_warn("sec %s flags mismatch\n", dst->sec_name);
990 if (dst->shdr->sh_entsize != src->shdr->sh_entsize) {
991 pr_warn("sec %s entsize mismatch\n", dst->sec_name);
998 static bool sec_content_is_same(struct dst_sec *dst_sec, struct src_sec *src_sec)
1000 if (dst_sec->sec_sz != src_sec->shdr->sh_size)
1002 if (memcmp(dst_sec->raw_data, src_sec->data->d_buf, dst_sec->sec_sz) != 0)
1007 static int extend_sec(struct dst_sec *dst, struct src_sec *src)
1010 size_t dst_align = dst->shdr->sh_addralign;
1011 size_t src_align = src->shdr->sh_addralign;
1012 size_t dst_align_sz, dst_final_sz;
1016 if (dst_align < src_align)
1017 dst_align = src_align;
1019 dst_align_sz = (dst->sec_sz + dst_align - 1) / dst_align * dst_align;
1021 /* no need to re-align final size */
1022 dst_final_sz = dst_align_sz + src->shdr->sh_size;
1024 if (src->shdr->sh_type != SHT_NOBITS) {
1025 tmp = realloc(dst->raw_data, dst_final_sz);
1028 dst->raw_data = tmp;
1030 /* pad dst section, if it's alignment forced size increase */
1031 memset(dst->raw_data + dst->sec_sz, 0, dst_align_sz - dst->sec_sz);
1032 /* now copy src data at a properly aligned offset */
1033 memcpy(dst->raw_data + dst_align_sz, src->data->d_buf, src->shdr->sh_size);
1036 dst->sec_sz = dst_final_sz;
1037 dst->shdr->sh_size = dst_final_sz;
1038 dst->data->d_size = dst_final_sz;
1040 dst->shdr->sh_addralign = dst_align;
1041 dst->data->d_align = dst_align;
1043 src->dst_off = dst_align_sz;
1048 static bool is_data_sec(struct src_sec *sec)
1050 if (!sec || sec->skipped)
1052 /* ephemeral sections are data sections, e.g., .kconfig, .ksyms */
1055 return sec->shdr->sh_type == SHT_PROGBITS || sec->shdr->sh_type == SHT_NOBITS;
1058 static bool is_relo_sec(struct src_sec *sec)
1060 if (!sec || sec->skipped || sec->ephemeral)
1062 return sec->shdr->sh_type == SHT_REL;
1065 static int linker_append_sec_data(struct bpf_linker *linker, struct src_obj *obj)
1069 for (i = 1; i < obj->sec_cnt; i++) {
1070 struct src_sec *src_sec;
1071 struct dst_sec *dst_sec;
1073 src_sec = &obj->secs[i];
1074 if (!is_data_sec(src_sec))
1077 dst_sec = find_dst_sec_by_name(linker, src_sec->sec_name);
1079 dst_sec = add_dst_sec(linker, src_sec->sec_name);
1082 err = init_sec(linker, dst_sec, src_sec);
1084 pr_warn("failed to init section '%s'\n", src_sec->sec_name);
1088 if (!secs_match(dst_sec, src_sec)) {
1089 pr_warn("ELF sections %s are incompatible\n", src_sec->sec_name);
1093 /* "license" and "version" sections are deduped */
1094 if (strcmp(src_sec->sec_name, "license") == 0
1095 || strcmp(src_sec->sec_name, "version") == 0) {
1096 if (!sec_content_is_same(dst_sec, src_sec)) {
1097 pr_warn("non-identical contents of section '%s' are not supported\n", src_sec->sec_name);
1100 src_sec->skipped = true;
1101 src_sec->dst_id = dst_sec->id;
1106 /* record mapped section index */
1107 src_sec->dst_id = dst_sec->id;
1109 if (src_sec->ephemeral)
1112 err = extend_sec(dst_sec, src_sec);
1120 static int linker_append_elf_syms(struct bpf_linker *linker, struct src_obj *obj)
1122 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
1123 Elf64_Sym *sym = symtab->data->d_buf, *dst_sym;
1124 int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize;
1125 int str_sec_idx = symtab->shdr->sh_link;
1127 obj->sym_map = calloc(n + 1, sizeof(*obj->sym_map));
1131 for (i = 0; i < n; i++, sym++) {
1132 struct src_sec *src_sec = NULL;
1133 struct dst_sec *dst_sec = NULL;
1134 const char *sym_name;
1138 /* we already have all-zero initial symbol */
1139 if (sym->st_name == 0 && sym->st_info == 0 &&
1140 sym->st_other == 0 && sym->st_shndx == SHN_UNDEF &&
1141 sym->st_value == 0 && sym->st_size ==0)
1144 sym_name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
1146 pr_warn("can't fetch symbol name for symbol #%d in '%s'\n", i, obj->filename);
1150 if (sym->st_shndx && sym->st_shndx < SHN_LORESERVE) {
1151 src_sec = &obj->secs[sym->st_shndx];
1152 if (src_sec->skipped)
1154 dst_sec = &linker->secs[src_sec->dst_id];
1156 /* allow only one STT_SECTION symbol per section */
1157 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && dst_sec->sec_sym_idx) {
1158 obj->sym_map[i] = dst_sec->sec_sym_idx;
1163 name_off = strset__add_str(linker->strtab_strs, sym_name);
1167 dst_sym = add_new_sym(linker, &dst_sym_idx);
1171 dst_sym->st_name = name_off;
1172 dst_sym->st_info = sym->st_info;
1173 dst_sym->st_other = sym->st_other;
1174 dst_sym->st_shndx = src_sec ? dst_sec->sec_idx : sym->st_shndx;
1175 dst_sym->st_value = (src_sec ? src_sec->dst_off : 0) + sym->st_value;
1176 dst_sym->st_size = sym->st_size;
1178 obj->sym_map[i] = dst_sym_idx;
1180 if (ELF64_ST_TYPE(sym->st_info) == STT_SECTION && dst_sym) {
1181 dst_sec->sec_sym_idx = dst_sym_idx;
1182 dst_sym->st_value = 0;
1190 static int linker_append_elf_relos(struct bpf_linker *linker, struct src_obj *obj)
1192 struct src_sec *src_symtab = &obj->secs[obj->symtab_sec_idx];
1193 struct dst_sec *dst_symtab = &linker->secs[linker->symtab_sec_idx];
1196 for (i = 1; i < obj->sec_cnt; i++) {
1197 struct src_sec *src_sec, *src_linked_sec;
1198 struct dst_sec *dst_sec, *dst_linked_sec;
1199 Elf64_Rel *src_rel, *dst_rel;
1202 src_sec = &obj->secs[i];
1203 if (!is_relo_sec(src_sec))
1206 /* shdr->sh_info points to relocatable section */
1207 src_linked_sec = &obj->secs[src_sec->shdr->sh_info];
1208 if (src_linked_sec->skipped)
1211 dst_sec = find_dst_sec_by_name(linker, src_sec->sec_name);
1213 dst_sec = add_dst_sec(linker, src_sec->sec_name);
1216 err = init_sec(linker, dst_sec, src_sec);
1218 pr_warn("failed to init section '%s'\n", src_sec->sec_name);
1221 } else if (!secs_match(dst_sec, src_sec)) {
1222 pr_warn("sections %s are not compatible\n", src_sec->sec_name);
1226 /* shdr->sh_link points to SYMTAB */
1227 dst_sec->shdr->sh_link = linker->symtab_sec_idx;
1229 /* shdr->sh_info points to relocated section */
1230 dst_linked_sec = &linker->secs[src_linked_sec->dst_id];
1231 dst_sec->shdr->sh_info = dst_linked_sec->sec_idx;
1233 src_sec->dst_id = dst_sec->id;
1234 err = extend_sec(dst_sec, src_sec);
1238 src_rel = src_sec->data->d_buf;
1239 dst_rel = dst_sec->raw_data + src_sec->dst_off;
1240 n = src_sec->shdr->sh_size / src_sec->shdr->sh_entsize;
1241 for (j = 0; j < n; j++, src_rel++, dst_rel++) {
1242 size_t src_sym_idx = ELF64_R_SYM(src_rel->r_info);
1243 size_t sym_type = ELF64_R_TYPE(src_rel->r_info);
1244 Elf64_Sym *src_sym, *dst_sym;
1247 src_sym_idx = ELF64_R_SYM(src_rel->r_info);
1248 src_sym = src_symtab->data->d_buf + sizeof(*src_sym) * src_sym_idx;
1250 dst_sym_idx = obj->sym_map[src_sym_idx];
1251 dst_sym = dst_symtab->raw_data + sizeof(*dst_sym) * dst_sym_idx;
1252 dst_rel->r_offset += src_linked_sec->dst_off;
1253 sym_type = ELF64_R_TYPE(src_rel->r_info);
1254 dst_rel->r_info = ELF64_R_INFO(dst_sym_idx, sym_type);
1256 if (ELF64_ST_TYPE(src_sym->st_info) == STT_SECTION) {
1257 struct src_sec *sec = &obj->secs[src_sym->st_shndx];
1258 struct bpf_insn *insn;
1260 if (src_linked_sec->shdr->sh_flags & SHF_EXECINSTR) {
1261 /* calls to the very first static function inside
1262 * .text section at offset 0 will
1263 * reference section symbol, not the
1264 * function symbol. Fix that up,
1265 * otherwise it won't be possible to
1266 * relocate calls to two different
1267 * static functions with the same name
1268 * (rom two different object files)
1270 insn = dst_linked_sec->raw_data + dst_rel->r_offset;
1271 if (insn->code == (BPF_JMP | BPF_CALL))
1272 insn->imm += sec->dst_off / sizeof(struct bpf_insn);
1274 insn->imm += sec->dst_off;
1276 pr_warn("relocation against STT_SECTION in non-exec section is not supported!\n");
1287 static struct src_sec *find_src_sec_by_name(struct src_obj *obj, const char *sec_name)
1289 struct src_sec *sec;
1292 for (i = 1; i < obj->sec_cnt; i++) {
1293 sec = &obj->secs[i];
1295 if (strcmp(sec->sec_name, sec_name) == 0)
1302 static Elf64_Sym *find_sym_by_name(struct src_obj *obj, size_t sec_idx,
1303 int sym_type, const char *sym_name)
1305 struct src_sec *symtab = &obj->secs[obj->symtab_sec_idx];
1306 Elf64_Sym *sym = symtab->data->d_buf;
1307 int i, n = symtab->shdr->sh_size / symtab->shdr->sh_entsize;
1308 int str_sec_idx = symtab->shdr->sh_link;
1311 for (i = 0; i < n; i++, sym++) {
1312 if (sym->st_shndx != sec_idx)
1314 if (ELF64_ST_TYPE(sym->st_info) != sym_type)
1317 name = elf_strptr(obj->elf, str_sec_idx, sym->st_name);
1321 if (strcmp(sym_name, name) != 0)
1330 static int linker_fixup_btf(struct src_obj *obj)
1332 const char *sec_name;
1333 struct src_sec *sec;
1339 n = btf__get_nr_types(obj->btf);
1340 for (i = 1; i <= n; i++) {
1341 struct btf_var_secinfo *vi;
1344 t = btf_type_by_id(obj->btf, i);
1345 if (btf_kind(t) != BTF_KIND_DATASEC)
1348 sec_name = btf__str_by_offset(obj->btf, t->name_off);
1349 sec = find_src_sec_by_name(obj, sec_name);
1351 /* record actual section size, unless ephemeral */
1353 t->size = sec->shdr->sh_size;
1355 /* BTF can have some sections that are not represented
1356 * in ELF, e.g., .kconfig and .ksyms, which are used
1357 * for special extern variables. Here we'll
1358 * pre-create "section shells" for them to be able to
1359 * keep track of extra per-section metadata later
1360 * (e.g., BTF variables).
1362 sec = add_src_sec(obj, sec_name);
1366 sec->ephemeral = true;
1367 sec->sec_idx = 0; /* will match UNDEF shndx in ELF */
1370 /* remember ELF section and its BTF type ID match */
1371 sec->sec_type_id = i;
1373 /* fix up variable offsets */
1374 vi = btf_var_secinfos(t);
1375 for (j = 0, m = btf_vlen(t); j < m; j++, vi++) {
1376 const struct btf_type *vt = btf__type_by_id(obj->btf, vi->type);
1377 const char *var_name = btf__str_by_offset(obj->btf, vt->name_off);
1378 int var_linkage = btf_var(vt)->linkage;
1381 /* no need to patch up static or extern vars */
1382 if (var_linkage != BTF_VAR_GLOBAL_ALLOCATED)
1385 sym = find_sym_by_name(obj, sec->sec_idx, STT_OBJECT, var_name);
1387 pr_warn("failed to find symbol for variable '%s' in section '%s'\n", var_name, sec_name);
1391 vi->offset = sym->st_value;
1398 static int remap_type_id(__u32 *type_id, void *ctx)
1402 *type_id = id_map[*type_id];
1407 static int linker_append_btf(struct bpf_linker *linker, struct src_obj *obj)
1409 const struct btf_type *t;
1410 int i, j, n, start_id, id;
1415 start_id = btf__get_nr_types(linker->btf) + 1;
1416 n = btf__get_nr_types(obj->btf);
1418 obj->btf_type_map = calloc(n + 1, sizeof(int));
1419 if (!obj->btf_type_map)
1422 for (i = 1; i <= n; i++) {
1423 t = btf__type_by_id(obj->btf, i);
1425 /* DATASECs are handled specially below */
1426 if (btf_kind(t) == BTF_KIND_DATASEC)
1429 id = btf__add_type(linker->btf, obj->btf, t);
1431 pr_warn("failed to append BTF type #%d from file '%s'\n", i, obj->filename);
1435 obj->btf_type_map[i] = id;
1438 /* remap all the types except DATASECs */
1439 n = btf__get_nr_types(linker->btf);
1440 for (i = start_id; i <= n; i++) {
1441 struct btf_type *dst_t = btf_type_by_id(linker->btf, i);
1443 if (btf_type_visit_type_ids(dst_t, remap_type_id, obj->btf_type_map))
1447 /* append DATASEC info */
1448 for (i = 1; i < obj->sec_cnt; i++) {
1449 struct src_sec *src_sec;
1450 struct dst_sec *dst_sec;
1451 const struct btf_var_secinfo *src_var;
1452 struct btf_var_secinfo *dst_var;
1454 src_sec = &obj->secs[i];
1455 if (!src_sec->sec_type_id || src_sec->skipped)
1457 dst_sec = &linker->secs[src_sec->dst_id];
1459 /* Mark section as having BTF regardless of the presence of
1460 * variables. In some cases compiler might generate empty BTF
1461 * with no variables information. E.g., when promoting local
1462 * array/structure variable initial values and BPF object
1463 * file otherwise has no read-only static variables in
1464 * .rodata. We need to preserve such empty BTF and just set
1465 * correct section size.
1467 dst_sec->has_btf = true;
1469 t = btf__type_by_id(obj->btf, src_sec->sec_type_id);
1470 src_var = btf_var_secinfos(t);
1472 for (j = 0; j < n; j++, src_var++) {
1473 void *sec_vars = dst_sec->sec_vars;
1475 sec_vars = libbpf_reallocarray(sec_vars,
1476 dst_sec->sec_var_cnt + 1,
1477 sizeof(*dst_sec->sec_vars));
1481 dst_sec->sec_vars = sec_vars;
1482 dst_sec->sec_var_cnt++;
1484 dst_var = &dst_sec->sec_vars[dst_sec->sec_var_cnt - 1];
1485 dst_var->type = obj->btf_type_map[src_var->type];
1486 dst_var->size = src_var->size;
1487 dst_var->offset = src_sec->dst_off + src_var->offset;
1494 static void *add_btf_ext_rec(struct btf_ext_sec_data *ext_data, const void *src_rec)
1498 tmp = libbpf_reallocarray(ext_data->recs, ext_data->rec_cnt + 1, ext_data->rec_sz);
1501 ext_data->recs = tmp;
1503 tmp += ext_data->rec_cnt * ext_data->rec_sz;
1504 memcpy(tmp, src_rec, ext_data->rec_sz);
1506 ext_data->rec_cnt++;
1511 static int linker_append_btf_ext(struct bpf_linker *linker, struct src_obj *obj)
1513 const struct btf_ext_info_sec *ext_sec;
1514 const char *sec_name, *s;
1515 struct src_sec *src_sec;
1516 struct dst_sec *dst_sec;
1517 int rec_sz, str_off, i;
1522 rec_sz = obj->btf_ext->func_info.rec_size;
1523 for_each_btf_ext_sec(&obj->btf_ext->func_info, ext_sec) {
1524 struct bpf_func_info_min *src_rec, *dst_rec;
1526 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
1527 src_sec = find_src_sec_by_name(obj, sec_name);
1529 pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
1532 dst_sec = &linker->secs[src_sec->dst_id];
1534 if (dst_sec->func_info.rec_sz == 0)
1535 dst_sec->func_info.rec_sz = rec_sz;
1536 if (dst_sec->func_info.rec_sz != rec_sz) {
1537 pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
1541 for_each_btf_ext_rec(&obj->btf_ext->func_info, ext_sec, i, src_rec) {
1542 dst_rec = add_btf_ext_rec(&dst_sec->func_info, src_rec);
1546 dst_rec->insn_off += src_sec->dst_off;
1547 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
1551 rec_sz = obj->btf_ext->line_info.rec_size;
1552 for_each_btf_ext_sec(&obj->btf_ext->line_info, ext_sec) {
1553 struct bpf_line_info_min *src_rec, *dst_rec;
1555 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
1556 src_sec = find_src_sec_by_name(obj, sec_name);
1558 pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
1561 dst_sec = &linker->secs[src_sec->dst_id];
1563 if (dst_sec->line_info.rec_sz == 0)
1564 dst_sec->line_info.rec_sz = rec_sz;
1565 if (dst_sec->line_info.rec_sz != rec_sz) {
1566 pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
1570 for_each_btf_ext_rec(&obj->btf_ext->line_info, ext_sec, i, src_rec) {
1571 dst_rec = add_btf_ext_rec(&dst_sec->line_info, src_rec);
1575 dst_rec->insn_off += src_sec->dst_off;
1577 s = btf__str_by_offset(obj->btf, src_rec->file_name_off);
1578 str_off = btf__add_str(linker->btf, s);
1581 dst_rec->file_name_off = str_off;
1583 s = btf__str_by_offset(obj->btf, src_rec->line_off);
1584 str_off = btf__add_str(linker->btf, s);
1587 dst_rec->line_off = str_off;
1589 /* dst_rec->line_col is fine */
1593 rec_sz = obj->btf_ext->core_relo_info.rec_size;
1594 for_each_btf_ext_sec(&obj->btf_ext->core_relo_info, ext_sec) {
1595 struct bpf_core_relo *src_rec, *dst_rec;
1597 sec_name = btf__name_by_offset(obj->btf, ext_sec->sec_name_off);
1598 src_sec = find_src_sec_by_name(obj, sec_name);
1600 pr_warn("can't find section '%s' referenced from .BTF.ext\n", sec_name);
1603 dst_sec = &linker->secs[src_sec->dst_id];
1605 if (dst_sec->core_relo_info.rec_sz == 0)
1606 dst_sec->core_relo_info.rec_sz = rec_sz;
1607 if (dst_sec->core_relo_info.rec_sz != rec_sz) {
1608 pr_warn("incompatible .BTF.ext record sizes for section '%s'\n", sec_name);
1612 for_each_btf_ext_rec(&obj->btf_ext->core_relo_info, ext_sec, i, src_rec) {
1613 dst_rec = add_btf_ext_rec(&dst_sec->core_relo_info, src_rec);
1617 dst_rec->insn_off += src_sec->dst_off;
1618 dst_rec->type_id = obj->btf_type_map[dst_rec->type_id];
1620 s = btf__str_by_offset(obj->btf, src_rec->access_str_off);
1621 str_off = btf__add_str(linker->btf, s);
1624 dst_rec->access_str_off = str_off;
1626 /* dst_rec->kind is fine */
1633 int bpf_linker__finalize(struct bpf_linker *linker)
1635 struct dst_sec *sec;
1643 err = finalize_btf(linker);
1647 /* Finalize strings */
1648 strs_sz = strset__data_size(linker->strtab_strs);
1649 strs = strset__data(linker->strtab_strs);
1651 sec = &linker->secs[linker->strtab_sec_idx];
1652 sec->data->d_align = 1;
1653 sec->data->d_off = 0LL;
1654 sec->data->d_buf = (void *)strs;
1655 sec->data->d_type = ELF_T_BYTE;
1656 sec->data->d_size = strs_sz;
1657 sec->shdr->sh_size = strs_sz;
1659 for (i = 1; i < linker->sec_cnt; i++) {
1660 sec = &linker->secs[i];
1662 /* STRTAB is handled specially above */
1663 if (sec->sec_idx == linker->strtab_sec_idx)
1666 /* special ephemeral sections (.ksyms, .kconfig, etc) */
1670 sec->data->d_buf = sec->raw_data;
1673 /* Finalize ELF layout */
1674 if (elf_update(linker->elf, ELF_C_NULL) < 0) {
1676 pr_warn_elf("failed to finalize ELF layout");
1680 /* Write out final ELF contents */
1681 if (elf_update(linker->elf, ELF_C_WRITE) < 0) {
1683 pr_warn_elf("failed to write ELF contents");
1687 elf_end(linker->elf);
1696 static int emit_elf_data_sec(struct bpf_linker *linker, const char *sec_name,
1697 size_t align, const void *raw_data, size_t raw_sz)
1704 name_off = strset__add_str(linker->strtab_strs, sec_name);
1708 scn = elf_newscn(linker->elf);
1711 data = elf_newdata(scn);
1714 shdr = elf64_getshdr(scn);
1718 shdr->sh_name = name_off;
1719 shdr->sh_type = SHT_PROGBITS;
1721 shdr->sh_size = raw_sz;
1724 shdr->sh_addralign = align;
1725 shdr->sh_entsize = 0;
1727 data->d_type = ELF_T_BYTE;
1728 data->d_size = raw_sz;
1729 data->d_buf = (void *)raw_data;
1730 data->d_align = align;
1736 static int finalize_btf(struct bpf_linker *linker)
1738 struct btf *btf = linker->btf;
1739 const void *raw_data;
1743 /* bail out if no BTF data was produced */
1744 if (btf__get_nr_types(linker->btf) == 0)
1747 for (i = 1; i < linker->sec_cnt; i++) {
1748 struct dst_sec *sec = &linker->secs[i];
1753 id = btf__add_datasec(btf, sec->sec_name, sec->sec_sz);
1755 pr_warn("failed to add consolidated BTF type for datasec '%s': %d\n",
1760 for (j = 0; j < sec->sec_var_cnt; j++) {
1761 struct btf_var_secinfo *vi = &sec->sec_vars[j];
1763 if (btf__add_datasec_var_info(btf, vi->type, vi->offset, vi->size))
1768 err = finalize_btf_ext(linker);
1770 pr_warn(".BTF.ext generation failed: %d\n", err);
1774 err = btf__dedup(linker->btf, linker->btf_ext, NULL);
1776 pr_warn("BTF dedup failed: %d\n", err);
1780 /* Emit .BTF section */
1781 raw_data = btf__get_raw_data(linker->btf, &raw_sz);
1785 err = emit_elf_data_sec(linker, BTF_ELF_SEC, 8, raw_data, raw_sz);
1787 pr_warn("failed to write out .BTF ELF section: %d\n", err);
1791 /* Emit .BTF.ext section */
1792 if (linker->btf_ext) {
1793 raw_data = btf_ext__get_raw_data(linker->btf_ext, &raw_sz);
1797 err = emit_elf_data_sec(linker, BTF_EXT_ELF_SEC, 8, raw_data, raw_sz);
1799 pr_warn("failed to write out .BTF.ext ELF section: %d\n", err);
1807 static int emit_btf_ext_data(struct bpf_linker *linker, void *output,
1808 const char *sec_name, struct btf_ext_sec_data *sec_data)
1810 struct btf_ext_info_sec *sec_info;
1815 if (!sec_data->rec_cnt)
1818 str_off = btf__add_str(linker->btf, sec_name);
1823 sec_info->sec_name_off = str_off;
1824 sec_info->num_info = sec_data->rec_cnt;
1825 cur += sizeof(struct btf_ext_info_sec);
1827 sz = sec_data->rec_cnt * sec_data->rec_sz;
1828 memcpy(cur, sec_data->recs, sz);
1831 return cur - output;
1834 static int finalize_btf_ext(struct bpf_linker *linker)
1836 size_t funcs_sz = 0, lines_sz = 0, core_relos_sz = 0, total_sz = 0;
1837 size_t func_rec_sz = 0, line_rec_sz = 0, core_relo_rec_sz = 0;
1838 struct btf_ext_header *hdr;
1842 /* validate that all sections have the same .BTF.ext record sizes
1843 * and calculate total data size for each type of data (func info,
1844 * line info, core relos)
1846 for (i = 1; i < linker->sec_cnt; i++) {
1847 struct dst_sec *sec = &linker->secs[i];
1849 if (sec->func_info.rec_cnt) {
1850 if (func_rec_sz == 0)
1851 func_rec_sz = sec->func_info.rec_sz;
1852 if (func_rec_sz != sec->func_info.rec_sz) {
1853 pr_warn("mismatch in func_info record size %zu != %u\n",
1854 func_rec_sz, sec->func_info.rec_sz);
1858 funcs_sz += sizeof(struct btf_ext_info_sec) + func_rec_sz * sec->func_info.rec_cnt;
1860 if (sec->line_info.rec_cnt) {
1861 if (line_rec_sz == 0)
1862 line_rec_sz = sec->line_info.rec_sz;
1863 if (line_rec_sz != sec->line_info.rec_sz) {
1864 pr_warn("mismatch in line_info record size %zu != %u\n",
1865 line_rec_sz, sec->line_info.rec_sz);
1869 lines_sz += sizeof(struct btf_ext_info_sec) + line_rec_sz * sec->line_info.rec_cnt;
1871 if (sec->core_relo_info.rec_cnt) {
1872 if (core_relo_rec_sz == 0)
1873 core_relo_rec_sz = sec->core_relo_info.rec_sz;
1874 if (core_relo_rec_sz != sec->core_relo_info.rec_sz) {
1875 pr_warn("mismatch in core_relo_info record size %zu != %u\n",
1876 core_relo_rec_sz, sec->core_relo_info.rec_sz);
1880 core_relos_sz += sizeof(struct btf_ext_info_sec) + core_relo_rec_sz * sec->core_relo_info.rec_cnt;
1884 if (!funcs_sz && !lines_sz && !core_relos_sz)
1887 total_sz += sizeof(struct btf_ext_header);
1889 funcs_sz += sizeof(__u32); /* record size prefix */
1890 total_sz += funcs_sz;
1893 lines_sz += sizeof(__u32); /* record size prefix */
1894 total_sz += lines_sz;
1896 if (core_relos_sz) {
1897 core_relos_sz += sizeof(__u32); /* record size prefix */
1898 total_sz += core_relos_sz;
1901 cur = data = calloc(1, total_sz);
1906 hdr->magic = BTF_MAGIC;
1907 hdr->version = BTF_VERSION;
1909 hdr->hdr_len = sizeof(struct btf_ext_header);
1910 cur += sizeof(struct btf_ext_header);
1912 /* All offsets are in bytes relative to the end of this header */
1913 hdr->func_info_off = 0;
1914 hdr->func_info_len = funcs_sz;
1915 hdr->line_info_off = funcs_sz;
1916 hdr->line_info_len = lines_sz;
1917 hdr->core_relo_off = funcs_sz + lines_sz;
1918 hdr->core_relo_len = core_relos_sz;
1921 *(__u32 *)cur = func_rec_sz;
1922 cur += sizeof(__u32);
1924 for (i = 1; i < linker->sec_cnt; i++) {
1925 struct dst_sec *sec = &linker->secs[i];
1927 sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->func_info);
1938 *(__u32 *)cur = line_rec_sz;
1939 cur += sizeof(__u32);
1941 for (i = 1; i < linker->sec_cnt; i++) {
1942 struct dst_sec *sec = &linker->secs[i];
1944 sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->line_info);
1954 if (core_relos_sz) {
1955 *(__u32 *)cur = core_relo_rec_sz;
1956 cur += sizeof(__u32);
1958 for (i = 1; i < linker->sec_cnt; i++) {
1959 struct dst_sec *sec = &linker->secs[i];
1961 sz = emit_btf_ext_data(linker, cur, sec->sec_name, &sec->core_relo_info);
1971 linker->btf_ext = btf_ext__new(data, total_sz);
1972 err = libbpf_get_error(linker->btf_ext);
1974 linker->btf_ext = NULL;
1975 pr_warn("failed to parse final .BTF.ext data: %d\n", err);