// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package xcoff implements access to XCOFF (Extended Common Object File Format) files.
package xcoff import ( ) // SectionHeader holds information about an XCOFF section header. type SectionHeader struct { Name string VirtualAddress uint64 Size uint64 Type uint32 Relptr uint64 Nreloc uint32 } type Section struct { SectionHeader Relocs []Reloc io.ReaderAt sr *io.SectionReader } // AuxiliaryCSect holds information about an XCOFF symbol in an AUX_CSECT entry. type AuxiliaryCSect struct { Length int64 StorageMappingClass int SymbolType int } // AuxiliaryFcn holds information about an XCOFF symbol in an AUX_FCN entry. type AuxiliaryFcn struct { Size int64 } type Symbol struct { Name string Value uint64 SectionNumber int StorageClass int AuxFcn AuxiliaryFcn AuxCSect AuxiliaryCSect } type Reloc struct { VirtualAddress uint64 Symbol *Symbol Signed bool InstructionFixed bool Length uint8 Type uint8 } // ImportedSymbol holds information about an imported XCOFF symbol. type ImportedSymbol struct { Name string Library string } // FileHeader holds information about an XCOFF file header. type FileHeader struct { TargetMachine uint16 } // A File represents an open XCOFF file. type File struct { FileHeader Sections []*Section Symbols []*Symbol StringTable []byte LibraryPaths []string closer io.Closer } // Open opens the named file using os.Open and prepares it for use as an XCOFF binary. func ( string) (*File, error) { , := os.Open() if != nil { return nil, } , := NewFile() if != nil { .Close() return nil, } .closer = return , nil } // Close closes the File. // If the File was created using NewFile directly instead of Open, // Close has no effect. func ( *File) () error { var error if .closer != nil { = .closer.Close() .closer = nil } return } // Section returns the first section with the given name, or nil if no such // section exists. // Xcoff have section's name limited to 8 bytes. Some sections like .gosymtab // can be trunked but this method will still find them. func ( *File) ( string) *Section { for , := range .Sections { if .Name == || (len() > 8 && .Name == [:8]) { return } } return nil } // SectionByType returns the first section in f with the // given type, or nil if there is no such section. func ( *File) ( uint32) *Section { for , := range .Sections { if .Type == { return } } return nil } // cstring converts ASCII byte sequence b to string. // It stops once it finds 0 or reaches end of b. func cstring( []byte) string { var int for = 0; < len() && [] != 0; ++ { } return string([:]) } // getString extracts a string from an XCOFF string table. func getString( []byte, uint32) (string, bool) { if < 4 || int() >= len() { return "", false } return cstring([:]), true } // NewFile creates a new File for accessing an XCOFF binary in an underlying reader. func ( io.ReaderAt) (*File, error) { := io.NewSectionReader(, 0, 1<<63-1) // Read XCOFF target machine var uint16 if := binary.Read(, binary.BigEndian, &); != nil { return nil, } if != U802TOCMAGIC && != U64_TOCMAGIC { return nil, fmt.Errorf("unrecognised XCOFF magic: 0x%x", ) } := new(File) .TargetMachine = // Read XCOFF file header if , := .Seek(0, io.SeekStart); != nil { return nil, } var uint16 var uint64 var uint32 var uint16 var int switch .TargetMachine { case U802TOCMAGIC: := new(FileHeader32) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } = .Fnscns = uint64(.Fsymptr) = .Fnsyms = .Fopthdr = FILHSZ_32 case U64_TOCMAGIC: := new(FileHeader64) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } = .Fnscns = .Fsymptr = .Fnsyms = .Fopthdr = FILHSZ_64 } if == 0 || <= 0 { return nil, fmt.Errorf("no symbol table") } // Read string table (located right after symbol table). := + uint64()*SYMESZ if , := .Seek(int64(), io.SeekStart); != nil { return nil, } // The first 4 bytes contain the length (in bytes). var uint32 if := binary.Read(, binary.BigEndian, &); != nil { return nil, } if > 4 { , := saferio.ReadDataAt(, uint64(), int64()) if != nil { return nil, } .StringTable = } // Read section headers if , := .Seek(int64()+int64(), io.SeekStart); != nil { return nil, } := saferio.SliceCap[*Section](uint64()) if < 0 { return nil, fmt.Errorf("too many XCOFF sections (%d)", ) } .Sections = make([]*Section, 0, ) for := 0; < int(); ++ { var uint64 := new(Section) switch .TargetMachine { case U802TOCMAGIC: := new(SectionHeader32) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .Name = cstring(.Sname[:]) .VirtualAddress = uint64(.Svaddr) .Size = uint64(.Ssize) = uint64(.Sscnptr) .Type = .Sflags .Relptr = uint64(.Srelptr) .Nreloc = uint32(.Snreloc) case U64_TOCMAGIC: := new(SectionHeader64) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .Name = cstring(.Sname[:]) .VirtualAddress = .Svaddr .Size = .Ssize = .Sscnptr .Type = .Sflags .Relptr = .Srelptr .Nreloc = .Snreloc } := if == 0 { // .bss must have all 0s = &nobitsSectionReader{} } .sr = io.NewSectionReader(, int64(), int64(.Size)) .ReaderAt = .sr .Sections = append(.Sections, ) } // Symbol map needed by relocation var = make(map[int]*Symbol) // Read symbol table if , := .Seek(int64(), io.SeekStart); != nil { return nil, } .Symbols = make([]*Symbol, 0) for := 0; < int(); ++ { var int var , bool := new(Symbol) switch .TargetMachine { case U802TOCMAGIC: := new(SymEnt32) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } = int(.Nnumaux) .SectionNumber = int(.Nscnum) .StorageClass = int(.Nsclass) .Value = uint64(.Nvalue) = .Ntype&SYM_TYPE_FUNC != 0 && > 1 := binary.BigEndian.Uint32(.Nname[:4]) if != 0 { .Name = cstring(.Nname[:]) } else { := binary.BigEndian.Uint32(.Nname[4:]) .Name, = getString(.StringTable, ) if ! { goto } } case U64_TOCMAGIC: := new(SymEnt64) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } = int(.Nnumaux) .SectionNumber = int(.Nscnum) .StorageClass = int(.Nsclass) .Value = .Nvalue = .Ntype&SYM_TYPE_FUNC != 0 && > 1 .Name, = getString(.StringTable, .Noffset) if ! { goto } } if .StorageClass != C_EXT && .StorageClass != C_WEAKEXT && .StorageClass != C_HIDEXT { goto } // Must have at least one csect auxiliary entry. if < 1 || + >= int() { goto } if .SectionNumber > int() { goto } if .SectionNumber == 0 { .Value = 0 } else { .Value -= .Sections[.SectionNumber-1].VirtualAddress } [] = // If this symbol is a function, it must retrieve its size from // its AUX_FCN entry. // It can happen that a function symbol doesn't have any AUX_FCN. // In this case, needAuxFcn is false and their size will be set to 0. if { switch .TargetMachine { case U802TOCMAGIC: := new(AuxFcn32) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .AuxFcn.Size = int64(.Xfsize) case U64_TOCMAGIC: := new(AuxFcn64) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .AuxFcn.Size = int64(.Xfsize) } } // Read csect auxiliary entry (by convention, it is the last). if ! { if , := .Seek(int64(-1)*SYMESZ, io.SeekCurrent); != nil { return nil, } } += = 0 switch .TargetMachine { case U802TOCMAGIC: := new(AuxCSect32) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .AuxCSect.SymbolType = int(.Xsmtyp & 0x7) .AuxCSect.StorageMappingClass = int(.Xsmclas) .AuxCSect.Length = int64(.Xscnlen) case U64_TOCMAGIC: := new(AuxCSect64) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .AuxCSect.SymbolType = int(.Xsmtyp & 0x7) .AuxCSect.StorageMappingClass = int(.Xsmclas) .AuxCSect.Length = int64(.Xscnlenhi)<<32 | int64(.Xscnlenlo) } .Symbols = append(.Symbols, ) : += // Skip auxiliary entries if , := .Seek(int64()*SYMESZ, io.SeekCurrent); != nil { return nil, } } // Read relocations // Only for .data or .text section for , := range .Sections { if .Type != STYP_TEXT && .Type != STYP_DATA { continue } if .Relptr == 0 { continue } := saferio.SliceCap[Reloc](uint64(.Nreloc)) if < 0 { return nil, fmt.Errorf("too many relocs (%d) for section %d", .Nreloc, ) } .Relocs = make([]Reloc, 0, ) if , := .Seek(int64(.Relptr), io.SeekStart); != nil { return nil, } for := uint32(0); < .Nreloc; ++ { var Reloc switch .TargetMachine { case U802TOCMAGIC: := new(Reloc32) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .VirtualAddress = uint64(.Rvaddr) .Symbol = [int(.Rsymndx)] .Type = .Rtype .Length = .Rsize&0x3F + 1 if .Rsize&0x80 != 0 { .Signed = true } if .Rsize&0x40 != 0 { .InstructionFixed = true } case U64_TOCMAGIC: := new(Reloc64) if := binary.Read(, binary.BigEndian, ); != nil { return nil, } .VirtualAddress = .Rvaddr .Symbol = [int(.Rsymndx)] .Type = .Rtype .Length = .Rsize&0x3F + 1 if .Rsize&0x80 != 0 { .Signed = true } if .Rsize&0x40 != 0 { .InstructionFixed = true } } .Relocs = append(.Relocs, ) } } return , nil } type nobitsSectionReader struct{} func (*nobitsSectionReader) ( []byte, int64) ( int, error) { return 0, errors.New("unexpected read from section with uninitialized data") } // Data reads and returns the contents of the XCOFF section s. func ( *Section) () ([]byte, error) { := make([]byte, .sr.Size()) , := .sr.ReadAt(, 0) if == len() { = nil } return [:], } // CSect reads and returns the contents of a csect. func ( *File) ( string) []byte { for , := range .Symbols { if .Name == && .AuxCSect.SymbolType == XTY_SD { if := .SectionNumber - 1; 0 <= && < len(.Sections) { := .Sections[] if .Value+uint64(.AuxCSect.Length) <= .Size { := make([]byte, .AuxCSect.Length) , := .sr.ReadAt(, int64(.Value)) if != nil { return nil } return } } break } } return nil } func ( *File) () (*dwarf.Data, error) { // There are many other DWARF sections, but these // are the ones the debug/dwarf package uses. // Don't bother loading others. var = [...]uint32{SSUBTYP_DWABREV, SSUBTYP_DWINFO, SSUBTYP_DWLINE, SSUBTYP_DWRNGES, SSUBTYP_DWSTR} var [len()][]byte for , := range { := .SectionByType(STYP_DWARF | ) if != nil { , := .Data() if != nil && uint64(len()) < .Size { return nil, } [] = } } , , , , := [0], [1], [2], [3], [4] return dwarf.New(, nil, nil, , , nil, , ) } // readImportID returns the import file IDs stored inside the .loader section. // Library name pattern is either path/base/member or base/member func ( *File) ( *Section) ([]string, error) { // Read loader header if , := .sr.Seek(0, io.SeekStart); != nil { return nil, } var uint32 var uint32 var uint64 switch .TargetMachine { case U802TOCMAGIC: := new(LoaderHeader32) if := binary.Read(.sr, binary.BigEndian, ); != nil { return nil, } = .Listlen = .Lnimpid = uint64(.Limpoff) case U64_TOCMAGIC: := new(LoaderHeader64) if := binary.Read(.sr, binary.BigEndian, ); != nil { return nil, } = .Listlen = .Lnimpid = .Limpoff } // Read loader import file ID table if , := .sr.Seek(int64(), io.SeekStart); != nil { return nil, } := make([]byte, ) if , := io.ReadFull(.sr, ); != nil { return nil, } := 0 // First import file ID is the default LIBPATH value := cstring([:]) .LibraryPaths = strings.Split(, ":") += len() + 3 // 3 null bytes := make([]string, 0) for := 1; < int(); ++ { := cstring([:]) += len() + 1 := cstring([:]) += len() + 1 := cstring([:]) += len() + 1 var string if len() > 0 { = + "/" + + "/" + } else { = + "/" + } = append(, ) } return , nil } // ImportedSymbols returns the names of all symbols // referred to by the binary f that are expected to be // satisfied by other libraries at dynamic load time. // It does not return weak symbols. func ( *File) () ([]ImportedSymbol, error) { := .SectionByType(STYP_LOADER) if == nil { return nil, nil } // Read loader header if , := .sr.Seek(0, io.SeekStart); != nil { return nil, } var uint32 var uint64 var uint32 var uint64 switch .TargetMachine { case U802TOCMAGIC: := new(LoaderHeader32) if := binary.Read(.sr, binary.BigEndian, ); != nil { return nil, } = .Lstlen = uint64(.Lstoff) = .Lnsyms = LDHDRSZ_32 case U64_TOCMAGIC: := new(LoaderHeader64) if := binary.Read(.sr, binary.BigEndian, ); != nil { return nil, } = .Lstlen = .Lstoff = .Lnsyms = .Lsymoff } // Read loader section string table if , := .sr.Seek(int64(), io.SeekStart); != nil { return nil, } := make([]byte, ) if , := io.ReadFull(.sr, ); != nil { return nil, } // Read imported libraries , := .readImportIDs() if != nil { return nil, } // Read loader symbol table if , := .sr.Seek(int64(), io.SeekStart); != nil { return nil, } := make([]ImportedSymbol, 0) for := 0; < int(); ++ { var string var uint32 var bool switch .TargetMachine { case U802TOCMAGIC: := new(LoaderSymbol32) if := binary.Read(.sr, binary.BigEndian, ); != nil { return nil, } if .Lsmtype&0x40 == 0 { continue // Imported symbols only } := binary.BigEndian.Uint32(.Lname[:4]) if != 0 { = cstring(.Lname[:]) } else { := binary.BigEndian.Uint32(.Lname[4:]) , = getString(, ) if ! { continue } } = .Lifile case U64_TOCMAGIC: := new(LoaderSymbol64) if := binary.Read(.sr, binary.BigEndian, ); != nil { return nil, } if .Lsmtype&0x40 == 0 { continue // Imported symbols only } , = getString(, .Loffset) if ! { continue } = .Lifile } var ImportedSymbol .Name = if >= 1 && int() <= len() { .Library = [-1] } = append(, ) } return , nil } // ImportedLibraries returns the names of all libraries // referred to by the binary f that are expected to be // linked with the binary at dynamic link time. func ( *File) () ([]string, error) { := .SectionByType(STYP_LOADER) if == nil { return nil, nil } , := .readImportIDs() return , }