MCPcopy
hub / github.com/cilium/ebpf / loadBTFMaps

Method loadBTFMaps

elf_reader.go:904–978  ·  view source on GitHub ↗

loadBTFMaps iterates over all ELF sections marked as BTF map sections (like .maps) and parses them into MapSpecs. Dump the .maps section and any relocations with `readelf -x .maps -r `.

()

Source from the content-addressed store, hash-verified

902// (like .maps) and parses them into MapSpecs. Dump the .maps section and
903// any relocations with `readelf -x .maps -r <elf_file>`.
904func (ec *elfCode) loadBTFMaps() error {
905 for _, sec := range ec.sections {
906 if sec.kind != btfMapSection {
907 continue
908 }
909
910 if ec.btf == nil {
911 return fmt.Errorf("missing BTF")
912 }
913
914 if sec.ReaderAt == nil {
915 return fmt.Errorf("compressed BTF map section is not supported")
916 }
917
918 vars, err := ec.sectionVars(ec.btf, sec.Name)
919 if err != nil {
920 return fmt.Errorf("section %v: loading map variable BTF: %w", sec.Name, err)
921 }
922
923 if len(vars) != len(sec.symbols) {
924 return fmt.Errorf("section %v: contains %d symbols but %d btf.Vars", sec.Name, len(sec.symbols), len(vars))
925 }
926
927 syms := make(map[string]elf.Symbol)
928 for _, sym := range sec.symbols {
929 syms[sym.Name] = sym
930 }
931
932 for _, v := range vars {
933 name := v.Name
934
935 // Find the ELF symbol corresponding to this Var.
936 sym, ok := syms[name]
937 if !ok {
938 return fmt.Errorf("section %v: missing symbol for map %s", sec.Name, name)
939 }
940
941 if sym.Value > math.MaxUint32 || sym.Size > math.MaxUint32 {
942 return fmt.Errorf("symbol %s: offset or size exceeds 32 bits in section %s", sym.Name, sec.Name)
943 }
944 if sym.Value+sym.Size > sec.Size {
945 return fmt.Errorf("section %v: symbol %s: size goes out of bounds of section", sec.Name, name)
946 }
947
948 sr := internal.NewBufferedSectionReader(sec, int64(sym.Value), int64(sym.Size))
949
950 // The BTF metadata for each Var contains the full length of the map
951 // declaration, so read the corresponding amount of bytes from the ELF.
952 // This way, we can pinpoint which map declaration contains unexpected
953 // (and therefore unsupported) data.
954 if _, err = io.Copy(internal.DiscardZeroes{}, sr); err != nil {
955 return fmt.Errorf("section %v: map %s: initializing BTF map definitions: %w", sec.Name, name, internal.ErrNotSupported)
956 }
957
958 if ec.maps[name] != nil {
959 return fmt.Errorf("section %v: map %s already exists", sec.Name, name)
960 }
961

Callers 1

Calls 5

sectionVarsMethod · 0.95
NewBufferedSectionReaderFunction · 0.92
UnderlyingTypeFunction · 0.92
mapSpecFromBTFFunction · 0.85
CopyMethod · 0.45

Tested by

no test coverage detected