| | |
| | |
| | |
| |
|
| | package pe |
| |
|
| | import ( |
| | "encoding/binary" |
| | "fmt" |
| | "internal/saferio" |
| | "io" |
| | "strconv" |
| | ) |
| |
|
| | |
| | type SectionHeader32 struct { |
| | Name [8]uint8 |
| | VirtualSize uint32 |
| | VirtualAddress uint32 |
| | SizeOfRawData uint32 |
| | PointerToRawData uint32 |
| | PointerToRelocations uint32 |
| | PointerToLineNumbers uint32 |
| | NumberOfRelocations uint16 |
| | NumberOfLineNumbers uint16 |
| | Characteristics uint32 |
| | } |
| |
|
| | |
| | |
| | |
| | func (sh *SectionHeader32) fullName(st StringTable) (string, error) { |
| | if sh.Name[0] != '/' { |
| | return cstring(sh.Name[:]), nil |
| | } |
| | i, err := strconv.Atoi(cstring(sh.Name[1:])) |
| | if err != nil { |
| | return "", err |
| | } |
| | return st.String(uint32(i)) |
| | } |
| |
|
| | |
| |
|
| | |
| | |
| | type Reloc struct { |
| | VirtualAddress uint32 |
| | SymbolTableIndex uint32 |
| | Type uint16 |
| | } |
| |
|
| | func readRelocs(sh *SectionHeader, r io.ReadSeeker) ([]Reloc, error) { |
| | if sh.NumberOfRelocations <= 0 { |
| | return nil, nil |
| | } |
| | _, err := r.Seek(int64(sh.PointerToRelocations), io.SeekStart) |
| | if err != nil { |
| | return nil, fmt.Errorf("fail to seek to %q section relocations: %v", sh.Name, err) |
| | } |
| | relocs := make([]Reloc, sh.NumberOfRelocations) |
| | err = binary.Read(r, binary.LittleEndian, relocs) |
| | if err != nil { |
| | return nil, fmt.Errorf("fail to read section relocations: %v", err) |
| | } |
| | return relocs, nil |
| | } |
| |
|
| | |
| | |
| | type SectionHeader struct { |
| | Name string |
| | VirtualSize uint32 |
| | VirtualAddress uint32 |
| | Size uint32 |
| | Offset uint32 |
| | PointerToRelocations uint32 |
| | PointerToLineNumbers uint32 |
| | NumberOfRelocations uint16 |
| | NumberOfLineNumbers uint16 |
| | Characteristics uint32 |
| | } |
| |
|
| | |
| | type Section struct { |
| | SectionHeader |
| | Relocs []Reloc |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | io.ReaderAt |
| | sr *io.SectionReader |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func (s *Section) Data() ([]byte, error) { |
| | return saferio.ReadDataAt(s.sr, uint64(s.Size), 0) |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | func (s *Section) Open() io.ReadSeeker { |
| | return io.NewSectionReader(s.sr, 0, 1<<63-1) |
| | } |
| |
|
| | |
| | const ( |
| | IMAGE_SCN_CNT_CODE = 0x00000020 |
| | IMAGE_SCN_CNT_INITIALIZED_DATA = 0x00000040 |
| | IMAGE_SCN_CNT_UNINITIALIZED_DATA = 0x00000080 |
| | IMAGE_SCN_LNK_COMDAT = 0x00001000 |
| | IMAGE_SCN_MEM_DISCARDABLE = 0x02000000 |
| | IMAGE_SCN_MEM_EXECUTE = 0x20000000 |
| | IMAGE_SCN_MEM_READ = 0x40000000 |
| | IMAGE_SCN_MEM_WRITE = 0x80000000 |
| | ) |
| |
|