golang.org/toolchain@v0.0.1-go1.9rc2.windows-amd64/src/cmd/vendor/github.com/google/pprof/internal/elfexec/elfexec_test.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package elfexec
    16  
    17  import (
    18  	"debug/elf"
    19  	"testing"
    20  )
    21  
    22  func TestGetBase(t *testing.T) {
    23  
    24  	fhExec := &elf.FileHeader{
    25  		Type: elf.ET_EXEC,
    26  	}
    27  	fhRel := &elf.FileHeader{
    28  		Type: elf.ET_REL,
    29  	}
    30  	fhDyn := &elf.FileHeader{
    31  		Type: elf.ET_DYN,
    32  	}
    33  	lsOffset := &elf.ProgHeader{
    34  		Vaddr: 0x400000,
    35  		Off:   0x200000,
    36  	}
    37  	kernelHeader := &elf.ProgHeader{
    38  		Vaddr: 0xffffffff81000000,
    39  	}
    40  	ppc64KernelHeader := &elf.ProgHeader{
    41  		Vaddr: 0xc000000000000000,
    42  	}
    43  
    44  	testcases := []struct {
    45  		label                string
    46  		fh                   *elf.FileHeader
    47  		loadSegment          *elf.ProgHeader
    48  		stextOffset          *uint64
    49  		start, limit, offset uint64
    50  		want                 uint64
    51  		wanterr              bool
    52  	}{
    53  		{"exec", fhExec, nil, nil, 0x400000, 0, 0, 0, false},
    54  		{"exec offset", fhExec, lsOffset, nil, 0x400000, 0x800000, 0, 0, false},
    55  		{"exec offset 2", fhExec, lsOffset, nil, 0x200000, 0x600000, 0, 0, false},
    56  		{"exec nomap", fhExec, nil, nil, 0, 0, 0, 0, false},
    57  		{"exec kernel", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0xffffffff82000198, 0xffffffff83000198, 0, 0x1000000, false},
    58  		{"exec PPC64 kernel", fhExec, ppc64KernelHeader, uint64p(0xc000000000000000), 0xc000000000000000, 0xd00000001a730000, 0xc000000000000000, 0x0, false},
    59  		{"exec chromeos kernel", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0, 0x10197, 0, 0x7efffe68, false},
    60  		{"exec chromeos kernel 2", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0, 0x10198, 0, 0x7efffe68, false},
    61  		{"exec chromeos kernel 3", fhExec, kernelHeader, uint64p(0xffffffff81000198), 0x198, 0x100000, 0, 0x7f000000, false},
    62  		{"exec chromeos kernel 4", fhExec, kernelHeader, uint64p(0xffffffff81200198), 0x198, 0x100000, 0, 0x7ee00000, false},
    63  		{"exec chromeos kernel unremapped", fhExec, kernelHeader, uint64p(0xffffffff810001c8), 0xffffffff834001c8, 0xffffffffc0000000, 0xffffffff834001c8, 0x2400000, false},
    64  		{"dyn", fhDyn, nil, nil, 0x200000, 0x300000, 0, 0x200000, false},
    65  		{"dyn offset", fhDyn, lsOffset, nil, 0x0, 0x300000, 0, 0xFFFFFFFFFFC00000, false},
    66  		{"dyn nomap", fhDyn, nil, nil, 0x0, 0x0, 0, 0, false},
    67  		{"rel", fhRel, nil, nil, 0x2000000, 0x3000000, 0, 0x2000000, false},
    68  		{"rel nomap", fhRel, nil, nil, 0x0, ^uint64(0), 0, 0, false},
    69  		{"rel offset", fhRel, nil, nil, 0x100000, 0x200000, 0x1, 0, true},
    70  	}
    71  
    72  	for _, tc := range testcases {
    73  		base, err := GetBase(tc.fh, tc.loadSegment, tc.stextOffset, tc.start, tc.limit, tc.offset)
    74  		if err != nil {
    75  			if !tc.wanterr {
    76  				t.Errorf("%s: want no error, got %v", tc.label, err)
    77  			}
    78  			continue
    79  		}
    80  		if tc.wanterr {
    81  			t.Errorf("%s: want error, got nil", tc.label)
    82  			continue
    83  		}
    84  		if base != tc.want {
    85  			t.Errorf("%s: want %x, got %x", tc.label, tc.want, base)
    86  		}
    87  	}
    88  }
    89  
    90  func uint64p(n uint64) *uint64 {
    91  	return &n
    92  }