| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package loader
- import (
- "encoding/binary"
- "fmt"
- "io"
- )
- // SectionTable manages reading/writing section entries
- type SectionTable struct {
- Entries []SectionEntry
- }
- // FindSection returns the first section of given type
- func (t *SectionTable) FindSection(stype SectionType) (*SectionEntry, error) {
- for i := range t.Entries {
- if t.Entries[i].Type == stype {
- return &t.Entries[i], nil
- }
- }
- return nil, fmt.Errorf("section type %d not found", stype)
- }
- // HasSection checks if a section type exists
- func (t *SectionTable) HasSection(stype SectionType) bool {
- for i := range t.Entries {
- if t.Entries[i].Type == stype {
- return true
- }
- }
- return false
- }
- // ReadSectionTable reads the section table from a reader
- func ReadSectionTable(r io.Reader, count uint32) (*SectionTable, error) {
- table := &SectionTable{
- Entries: make([]SectionEntry, count),
- }
-
- for i := uint32(0); i < count; i++ {
- var entry SectionEntry
- if err := binary.Read(r, binary.LittleEndian, &entry.Type); err != nil {
- return nil, fmt.Errorf("reading section type: %w", err)
- }
- if err := binary.Read(r, binary.LittleEndian, &entry.Offset); err != nil {
- return nil, fmt.Errorf("reading section offset: %w", err)
- }
- if err := binary.Read(r, binary.LittleEndian, &entry.Size); err != nil {
- return nil, fmt.Errorf("reading section size: %w", err)
- }
- if err := binary.Read(r, binary.LittleEndian, &entry.Flags); err != nil {
- return nil, fmt.Errorf("reading section flags: %w", err)
- }
- table.Entries[i] = entry
- }
-
- return table, nil
- }
- // WriteSectionTable writes the section table to a writer
- func WriteSectionTable(w io.Writer, table *SectionTable) error {
- for _, entry := range table.Entries {
- if err := binary.Write(w, binary.LittleEndian, entry.Type); err != nil {
- return err
- }
- if err := binary.Write(w, binary.LittleEndian, entry.Offset); err != nil {
- return err
- }
- if err := binary.Write(w, binary.LittleEndian, entry.Size); err != nil {
- return err
- }
- if err := binary.Write(w, binary.LittleEndian, entry.Flags); err != nil {
- return err
- }
- }
- return nil
- }
- // SectionEntrySize is the byte size of a single section entry
- const SectionEntrySize = 4 + 8 + 8 + 4 // Type + Offset + Size + Flags = 24 bytes
|