]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/link: add new loading package for XCOFF files
authorClément Chigot <clement.chigot@atos.net>
Fri, 28 Sep 2018 14:19:18 +0000 (16:19 +0200)
committerIan Lance Taylor <iant@golang.org>
Tue, 23 Oct 2018 14:05:04 +0000 (14:05 +0000)
This commit adds the new package cmd/link/internal/loadxcoff.

It also adds a new symbol kind in cmd/link/internal/sym package, which
aims to represent TOC entries for XCOFF files.

cmd/dist is updated to add this new package and cmd/internal/xcoff during
the bootstrap.

Updates: #25893

Change-Id: I42b6578cf0ba4cc28ad4aa98122a91ab1d1bbf6e
Reviewed-on: https://go-review.googlesource.com/c/138728
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/dist/buildtool.go
src/cmd/link/internal/loadxcoff/ldxcoff.go [new file with mode: 0644]
src/cmd/link/internal/sym/symkind.go
src/cmd/link/internal/sym/symkind_string.go

index 2058c721c5a641beec7b8953798c8fb8d5f98421..2db115e20ede7d318a21e452d108b527ca37c91a 100644 (file)
@@ -74,6 +74,7 @@ var bootstrapDirs = []string{
        "cmd/link/internal/loadelf",
        "cmd/link/internal/loadmacho",
        "cmd/link/internal/loadpe",
+       "cmd/link/internal/loadxcoff",
        "cmd/link/internal/mips",
        "cmd/link/internal/mips64",
        "cmd/link/internal/objfile",
diff --git a/src/cmd/link/internal/loadxcoff/ldxcoff.go b/src/cmd/link/internal/loadxcoff/ldxcoff.go
new file mode 100644 (file)
index 0000000..7204d34
--- /dev/null
@@ -0,0 +1,225 @@
+// 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 loadxcoff implements a XCOFF file reader.
+package loadxcoff
+
+import (
+       "cmd/internal/bio"
+       "cmd/internal/objabi"
+       "cmd/internal/sys"
+       "cmd/internal/xcoff"
+       "cmd/link/internal/sym"
+       "errors"
+       "fmt"
+)
+
+// ldSection is an XCOFF section with its symbols.
+type ldSection struct {
+       xcoff.Section
+       sym *sym.Symbol
+}
+
+// TODO(brainman): maybe just add ReadAt method to bio.Reader instead of creating xcoffBiobuf
+
+// xcoffBiobuf makes bio.Reader look like io.ReaderAt.
+type xcoffBiobuf bio.Reader
+
+func (f *xcoffBiobuf) ReadAt(p []byte, off int64) (int, error) {
+       ret := ((*bio.Reader)(f)).Seek(off, 0)
+       if ret < 0 {
+               return 0, errors.New("fail to seek")
+       }
+       n, err := f.Read(p)
+       if err != nil {
+               return 0, err
+       }
+       return n, nil
+}
+
+// Load loads the Xcoff file pn from f.
+// Symbols are written into syms, and a slice of the text symbols is returned.
+func Load(arch *sys.Arch, syms *sym.Symbols, input *bio.Reader, pkg string, length int64, pn string) (textp []*sym.Symbol, err error) {
+       errorf := func(str string, args ...interface{}) ([]*sym.Symbol, error) {
+               return nil, fmt.Errorf("loadxcoff: %v: %v", pn, fmt.Sprintf(str, args...))
+       }
+       localSymVersion := syms.IncVersion()
+
+       var ldSections []*ldSection
+
+       f, err := xcoff.NewFile((*xcoffBiobuf)(input))
+       if err != nil {
+               return nil, err
+       }
+       defer f.Close()
+
+       for _, sect := range f.Sections {
+               //only text, data and bss section
+               if sect.Type < xcoff.STYP_TEXT || sect.Type > xcoff.STYP_BSS {
+                       continue
+               }
+               lds := new(ldSection)
+               lds.Section = *sect
+               name := fmt.Sprintf("%s(%s)", pkg, lds.Name)
+               s := syms.Lookup(name, localSymVersion)
+
+               switch lds.Type {
+               default:
+                       return errorf("unrecognized section type 0x%x", lds.Type)
+               case xcoff.STYP_TEXT:
+                       s.Type = sym.STEXT
+               case xcoff.STYP_DATA:
+                       s.Type = sym.SNOPTRDATA
+               case xcoff.STYP_BSS:
+                       s.Type = sym.SNOPTRBSS
+               }
+
+               s.Size = int64(lds.Size)
+               if s.Type != sym.SNOPTRBSS {
+                       data, err := lds.Section.Data()
+                       if err != nil {
+                               return nil, err
+                       }
+                       s.P = data
+               }
+
+               lds.sym = s
+               ldSections = append(ldSections, lds)
+       }
+
+       // sx = symbol from file
+       // s = symbol for syms
+       for _, sx := range f.Symbols {
+               // get symbol type
+               stype, errmsg := getSymbolType(f, sx)
+               if errmsg != "" {
+                       return errorf("error reading symbol %s: %s", sx.Name, errmsg)
+               }
+               if stype == sym.Sxxx {
+                       continue
+               }
+
+               s := syms.Lookup(sx.Name, 0)
+
+               // Text symbol
+               if s.Type == sym.STEXT {
+                       if s.Attr.OnList() {
+                               return errorf("symbol %s listed multiple times", s.Name)
+                       }
+                       s.Attr |= sym.AttrOnList
+                       textp = append(textp, s)
+               }
+       }
+
+       // Read relocations
+       for _, sect := range ldSections {
+               // TODO(aix): Dwarf section relocation if needed
+               if sect.Type != xcoff.STYP_TEXT && sect.Type != xcoff.STYP_DATA {
+                       continue
+               }
+               rs := make([]sym.Reloc, sect.Nreloc)
+               for i, rx := range sect.Relocs {
+                       r := &rs[i]
+
+                       r.Sym = syms.Lookup(rx.Symbol.Name, 0)
+                       if uint64(int32(rx.VirtualAddress)) != rx.VirtualAddress {
+                               return errorf("virtual address of a relocation is too big: 0x%x", rx.VirtualAddress)
+                       }
+                       r.Off = int32(rx.VirtualAddress)
+                       switch rx.Type {
+                       default:
+                               return errorf("section %s: unknown relocation of type 0x%x", sect.Name, rx.Type)
+                       case xcoff.R_POS:
+                               // Reloc the address of r.Sym
+                               // Length should be 64
+                               if rx.Length != 64 {
+                                       return errorf("section %s: relocation R_POS has length different from 64: %d", sect.Name, rx.Length)
+                               }
+                               r.Siz = 8
+                               r.Type = objabi.R_CONST
+                               r.Add = int64(rx.Symbol.Value)
+
+                       case xcoff.R_RBR:
+                               r.Siz = 4
+                               r.Type = objabi.R_CALLPOWER
+                               r.Add = 0 //
+
+                       }
+               }
+               s := sect.sym
+               s.R = rs
+               s.R = s.R[:sect.Nreloc]
+       }
+       return textp, nil
+
+}
+
+// Convert symbol xcoff type to sym.SymKind
+// Returns nil if this shouldn't be added into syms (like .file or .dw symbols )
+func getSymbolType(f *xcoff.File, s *xcoff.Symbol) (stype sym.SymKind, err string) {
+       // .file symbol
+       if s.SectionNumber == -2 {
+               if s.StorageClass == xcoff.C_FILE {
+                       return sym.Sxxx, ""
+               }
+               return sym.Sxxx, "unrecognised StorageClass for sectionNumber = -2"
+       }
+
+       // extern symbols
+       // TODO(aix)
+       if s.SectionNumber == 0 {
+               return sym.Sxxx, ""
+       }
+
+       sectType := f.Sections[s.SectionNumber-1].SectionHeader.Type
+       switch sectType {
+       default:
+               return sym.Sxxx, fmt.Sprintf("getSymbolType for Section type 0x%x not implemented", sectType)
+       case xcoff.STYP_DWARF, xcoff.STYP_DEBUG:
+               return sym.Sxxx, ""
+       case xcoff.STYP_DATA, xcoff.STYP_BSS, xcoff.STYP_TEXT:
+       }
+
+       switch s.StorageClass {
+       default:
+               return sym.Sxxx, fmt.Sprintf("getSymbolType for Storage class 0x%x not implemented", s.StorageClass)
+       case xcoff.C_HIDEXT, xcoff.C_EXT, xcoff.C_WEAKEXT:
+               switch s.AuxCSect.StorageMappingClass {
+               default:
+                       return sym.Sxxx, fmt.Sprintf("getSymbolType for Storage class 0x%x and Storage Map 0x%x not implemented", s.StorageClass, s.AuxCSect.StorageMappingClass)
+
+               // Program Code
+               case xcoff.XMC_PR:
+                       if sectType == xcoff.STYP_TEXT {
+                               return sym.STEXT, ""
+                       }
+                       return sym.Sxxx, fmt.Sprintf("unrecognised Section Type 0x%x for Storage Class 0x%x with Storage Map XMC_PR", sectType, s.StorageClass)
+
+               // Read/Write Data
+               case xcoff.XMC_RW:
+                       if sectType == xcoff.STYP_DATA {
+                               return sym.SDATA, ""
+                       }
+                       if sectType == xcoff.STYP_BSS {
+                               return sym.SBSS, ""
+                       }
+                       return sym.Sxxx, fmt.Sprintf("unrecognised Section Type 0x%x for Storage Class 0x%x with Storage Map XMC_RW", sectType, s.StorageClass)
+
+               // Function descriptor
+               case xcoff.XMC_DS:
+                       if sectType == xcoff.STYP_DATA {
+                               return sym.SDATA, ""
+                       }
+                       return sym.Sxxx, fmt.Sprintf("unrecognised Section Type 0x%x for Storage Class 0x%x with Storage Map XMC_DS", sectType, s.StorageClass)
+
+               // TOC anchor and TOC entry
+               case xcoff.XMC_TC0, xcoff.XMC_TE:
+                       if sectType == xcoff.STYP_DATA {
+                               return sym.SXCOFFTOC, ""
+                       }
+                       return sym.Sxxx, fmt.Sprintf("unrecognised Section Type 0x%x for Storage Class 0x%x with Storage Map XMC_DS", sectType, s.StorageClass)
+
+               }
+       }
+}
index 2e21cc1f00a8a9f12c10ea5ce0b7c8fb8bff4d65..b1756d6145e9e1af88084d9c85de1ef8ca814230 100644 (file)
@@ -92,6 +92,7 @@ const (
        SBSS
        SNOPTRBSS
        STLSBSS
+       SXCOFFTOC
        SXREF
        SMACHOSYMSTR
        SMACHOSYMTAB
index e7e56c4003d04cc605b21981599b29f82299915a..7428503b1c8a36e7fdb6ffd02ff6d7ceb3ee755e 100644 (file)
@@ -4,9 +4,9 @@ package sym
 
 import "strconv"
 
-const _SymKind_name = "SxxxSTEXTSELFRXSECTSTYPESSTRINGSGOSTRINGSGOFUNCSGCBITSSRODATASFUNCTABSELFROSECTSMACHOPLTSTYPERELROSSTRINGRELROSGOSTRINGRELROSGOFUNCRELROSGCBITSRELROSRODATARELROSFUNCTABRELROSTYPELINKSITABLINKSSYMTABSPCLNTABSELFSECTSMACHOSMACHOGOTSWINDOWSSELFGOTSNOPTRDATASINITARRSDATASBSSSNOPTRBSSSTLSBSSSXREFSMACHOSYMSTRSMACHOSYMTABSMACHOINDIRECTPLTSMACHOINDIRECTGOTSFILEPATHSCONSTSDYNIMPORTSHOSTOBJSDWARFSECTSDWARFINFOSDWARFRANGESDWARFLOCSDWARFMISC"
+const _SymKind_name = "SxxxSTEXTSELFRXSECTSTYPESSTRINGSGOSTRINGSGOFUNCSGCBITSSRODATASFUNCTABSELFROSECTSMACHOPLTSTYPERELROSSTRINGRELROSGOSTRINGRELROSGOFUNCRELROSGCBITSRELROSRODATARELROSFUNCTABRELROSTYPELINKSITABLINKSSYMTABSPCLNTABSELFSECTSMACHOSMACHOGOTSWINDOWSSELFGOTSNOPTRDATASINITARRSDATASBSSSNOPTRBSSSTLSBSSSXCOFFTOCSXREFSMACHOSYMSTRSMACHOSYMTABSMACHOINDIRECTPLTSMACHOINDIRECTGOTSFILEPATHSCONSTSDYNIMPORTSHOSTOBJSDWARFSECTSDWARFINFOSDWARFRANGESDWARFLOCSDWARFMISC"
 
-var _SymKind_index = [...]uint16{0, 4, 9, 19, 24, 31, 40, 47, 54, 61, 69, 79, 88, 98, 110, 124, 136, 148, 160, 173, 182, 191, 198, 206, 214, 220, 229, 237, 244, 254, 262, 267, 271, 280, 287, 292, 304, 316, 333, 350, 359, 365, 375, 383, 393, 403, 414, 423, 433}
+var _SymKind_index = [...]uint16{0, 4, 9, 19, 24, 31, 40, 47, 54, 61, 69, 79, 88, 98, 110, 124, 136, 148, 160, 173, 182, 191, 198, 206, 214, 220, 229, 237, 244, 254, 262, 267, 271, 280, 287, 296, 301, 313, 325, 342, 359, 368, 374, 384, 392, 402, 412, 423, 432, 442}
 
 func (i SymKind) String() string {
        if i >= SymKind(len(_SymKind_index)-1) {