github.com/dylandreimerink/gobpfld@v0.6.1-0.20220205171531-e79c330ad608/ebpf/store.go (about)

     1  package ebpf
     2  
     3  import "fmt"
     4  
     5  var _ Instruction = (*StoreMemoryConstant)(nil)
     6  
     7  type StoreMemoryConstant struct {
     8  	Dest   Register
     9  	Size   Size
    10  	Offset int16
    11  	Value  int32
    12  }
    13  
    14  func (sm *StoreMemoryConstant) Raw() ([]RawInstruction, error) {
    15  	return []RawInstruction{
    16  		{
    17  			Op:  BPF_ST | uint8(sm.Size) | BPF_MEM,
    18  			Reg: NewReg(0, sm.Dest),
    19  			Off: sm.Offset,
    20  			Imm: sm.Value,
    21  		},
    22  	}, nil
    23  }
    24  
    25  func (sm *StoreMemoryConstant) String() string {
    26  	sign := "+"
    27  	offset := sm.Offset
    28  	if offset < 0 {
    29  		sign = "-"
    30  		offset = -offset
    31  	}
    32  	return fmt.Sprintf("*(%s *)(r%s %s %d) = %d", sm.Size, sm.Dest, sign, offset, sm.Value)
    33  }
    34  
    35  var _ Instruction = (*StoreMemoryRegister)(nil)
    36  
    37  type StoreMemoryRegister struct {
    38  	Src    Register
    39  	Dest   Register
    40  	Offset int16
    41  	Size   Size
    42  }
    43  
    44  func (sm *StoreMemoryRegister) Raw() ([]RawInstruction, error) {
    45  	return []RawInstruction{
    46  		{
    47  			Op:  BPF_STX | uint8(sm.Size) | BPF_MEM,
    48  			Reg: NewReg(sm.Src, sm.Dest),
    49  			Off: sm.Offset,
    50  		},
    51  	}, nil
    52  }
    53  
    54  func (sm *StoreMemoryRegister) String() string {
    55  	sign := "+"
    56  	offset := sm.Offset
    57  	if offset < 0 {
    58  		sign = "-"
    59  		offset = -offset
    60  	}
    61  	return fmt.Sprintf("*(%s *)(r%s %s %d) = r%s", sm.Size, sm.Dest, sign, offset, sm.Src)
    62  }