| 792 | } |
| 793 | |
| 794 | func TestELFObjAddr(t *testing.T) { |
| 795 | // The exe_linux_64 has two loadable program headers: |
| 796 | // LOAD 0x0000000000000000 0x0000000000400000 0x0000000000400000 |
| 797 | // 0x00000000000006fc 0x00000000000006fc R E 0x200000 |
| 798 | // LOAD 0x0000000000000e10 0x0000000000600e10 0x0000000000600e10 |
| 799 | // 0x0000000000000230 0x0000000000000238 RW 0x200000 |
| 800 | name := filepath.Join("testdata", "exe_linux_64") |
| 801 | |
| 802 | for _, tc := range []struct { |
| 803 | desc string |
| 804 | start, limit, offset uint64 |
| 805 | wantOpenError bool |
| 806 | addr uint64 |
| 807 | wantObjAddr uint64 |
| 808 | wantAddrError bool |
| 809 | }{ |
| 810 | {"exec mapping, good address", 0x5400000, 0x5401000, 0, false, 0x5400400, 0x400400, false}, |
| 811 | {"exec mapping, address outside segment", 0x5400000, 0x5401000, 0, false, 0x5400800, 0, true}, |
| 812 | {"short data mapping, good address", 0x5600e00, 0x5602000, 0xe00, false, 0x5600e10, 0x600e10, false}, |
| 813 | {"short data mapping, address outside segment", 0x5600e00, 0x5602000, 0xe00, false, 0x5600e00, 0x600e00, false}, |
| 814 | {"page aligned data mapping, good address", 0x5600000, 0x5602000, 0, false, 0x5601000, 0x601000, false}, |
| 815 | {"page aligned data mapping, address outside segment", 0x5600000, 0x5602000, 0, false, 0x5601048, 0, true}, |
| 816 | {"bad file offset, no matching segment", 0x5600000, 0x5602000, 0x2000, false, 0x5600e10, 0, true}, |
| 817 | {"large mapping size, match by sample offset", 0x5600000, 0x5603000, 0, false, 0x5600e10, 0x600e10, false}, |
| 818 | } { |
| 819 | t.Run(tc.desc, func(t *testing.T) { |
| 820 | b := binrep{} |
| 821 | o, err := b.openELF(name, tc.start, tc.limit, tc.offset, "") |
| 822 | if (err != nil) != tc.wantOpenError { |
| 823 | t.Errorf("openELF got error %v, want any error=%v", err, tc.wantOpenError) |
| 824 | } |
| 825 | if err != nil { |
| 826 | return |
| 827 | } |
| 828 | got, err := o.ObjAddr(tc.addr) |
| 829 | if (err != nil) != tc.wantAddrError { |
| 830 | t.Errorf("ObjAddr got error %v, want any error=%v", err, tc.wantAddrError) |
| 831 | } |
| 832 | if err != nil { |
| 833 | return |
| 834 | } |
| 835 | if got != tc.wantObjAddr { |
| 836 | t.Errorf("got ObjAddr %x; want %x\n", got, tc.wantObjAddr) |
| 837 | } |
| 838 | }) |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | type buf struct { |
| 843 | data []byte |