<commit_before>// Inferno utils/8l/asm.c
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/8l/asm.c
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 <NAME> (<EMAIL>)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 <NAME>
//	Portions Copyright © 2005-2007 <NAME> (<EMAIL>)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package ld

import (
	"bufio"
	"bytes"
	"cmd/internal/bio"
	"cmd/internal/obj"
	"cmd/internal/objabi"
	"cmd/internal/sys"
	"cmd/link/internal/loadelf"
	"cmd/link/internal/loader"
	"cmd/link/internal/loadmacho"
	"cmd/link/internal/loadpe"
	"cmd/link/internal/loadxcoff"
	"cmd/link/internal/sym"
	"crypto/sha1"
	"debug/elf"
	"debug/macho"
	"encoding/base64"
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"sort"
	"strings"
	"sync"
)

// Data layout and relocation.

// Derived from Inferno utils/6l/l.h
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/l.h
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 <NAME> (<EMAIL>)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 <NAME>
//	Portions Copyright © 2005-2007 <NAME> (<EMAIL>)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

type LookupFn func(name string, version int) *sym.Symbol

// ArchSyms holds a number of architecture specific symbols used during
// relocation.  Rather than allowing them universal access to all symbols,
// we keep a subset for relocation application.
type ArchSyms struct {
	TOC    *sym.Symbol
	DotTOC []*sym.Symbol // for each version

	GOT    *sym.Symbol
	PLT    *sym.Symbol
	GOTPLT *sym.Symbol

	Tlsg      *sym.Symbol
	Tlsoffset int

	Dynamic *sym.Symbol
	DynSym  *sym.Symbol
	DynStr  *sym.Symbol

	// Elf specific
	Rel     *sym.Symbol
	Rela    *sym.Symbol
	RelPLT  *sym.Symbol
	RelaPLT *sym.Symbol

	// Darwin symbols
	LinkEditGOT *sym.Symbol
	LinkEditPLT *sym.Symbol
}

// setArchSyms sets up the ArchSyms structure, and must be called before
// relocations are applied.
func (ctxt *Link) setArchSyms() {
	ctxt.GOT = ctxt.Syms.Lookup(".got", 0)
	ctxt.PLT = ctxt.Syms.Lookup(".plt", 0)
	ctxt.GOTPLT = ctxt.Syms.Lookup(".got.plt", 0)

	ctxt.Dynamic = ctxt.Syms.Lookup(".dynamic", 0)
	ctxt.DynSym = ctxt.Syms.Lookup(".dynsym", 0)
	ctxt.DynStr = ctxt.Syms.Lookup(".dynstr", 0)

	if ctxt.IsAIX() {
		ctxt.TOC = ctxt.Syms.Lookup("TOC", 0)
		ctxt.DotTOC = make([]*sym.Symbol, ctxt.Syms.MaxVersion()+1)
		for i := 0; i <= ctxt.Syms.MaxVersion(); i++ {
			if i >= 2 && i < sym.SymVerStatic { // these versions are not used currently
				continue
			}
			ctxt.DotTOC[i] = ctxt.Syms.Lookup(".TOC.", i)
		}
	}
	if ctxt.IsElf() {
		ctxt.Rel = ctxt.Syms.Lookup(".rel", 0)
		ctxt.Rela = ctxt.Syms.Lookup(".rela", 0)
		ctxt.RelPLT = ctxt.Syms.Lookup(".rel.plt", 0)
		ctxt.RelaPLT = ctxt.Syms.Lookup(".rela.plt", 0)
	}
	if ctxt.IsDarwin() {
		ctxt.LinkEditGOT = ctxt.Syms.Lookup(".linkedit.got", 0)
		ctxt.LinkEditPLT = ctxt.Syms.Lookup(".linkedit.plt", 0)
	}
}

type Arch struct {
	Funcalign      int
	Maxalign       int
	Minalign       int
	Dwarfregsp     int
	Dwarfreglr     int
	Androiddynld   string
	Linuxdynld     string<commit_msg>[dev.link] cmd/link: add loader.Sym equivalents to ArchSyms<commit_after>// Inferno utils/8l/asm.c
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/8l/asm.c
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 <NAME> (<EMAIL>)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 <NAME>
//	Portions Copyright © 2005-2007 <NAME> (<EMAIL>)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package ld

import (
	"bufio"
	"bytes"
	"cmd/internal/bio"
	"cmd/internal/obj"
	"cmd/internal/objabi"
	"cmd/internal/sys"
	"cmd/link/internal/loadelf"
	"cmd/link/internal/loader"
	"cmd/link/internal/loadmacho"
	"cmd/link/internal/loadpe"
	"cmd/link/internal/loadxcoff"
	"cmd/link/internal/sym"
	"crypto/sha1"
	"debug/elf"
	"debug/macho"
	"encoding/base64"
	"encoding/binary"
	"encoding/hex"
	"fmt"
	"io"
	"io/ioutil"
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"sort"
	"strings"
	"sync"
)

// Data layout and relocation.

// Derived from Inferno utils/6l/l.h
// https://bitbucket.org/inferno-os/inferno-os/src/default/utils/6l/l.h
//
//	Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
//	Portions Copyright © 1995-1997 <NAME> (<EMAIL>)
//	Portions Copyright © 1997-1999 Vita Nuova Limited
//	Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
//	Portions Copyright © 2004,2006 <NAME>
//	Portions Copyright © 2005-2007 <NAME> (<EMAIL>)
//	Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
//	Portions Copyright © 2009 The Go Authors. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

type LookupFn func(name string, version int) *sym.Symbol

// ArchSyms holds a number of architecture specific symbols used during
// relocation.  Rather than allowing them universal access to all symbols,
// we keep a subset for relocation application.
type ArchSyms struct {
	TOC    *sym.Symbol
	DotTOC []*sym.Symbol // for each version

	GOT    *sym.Symbol
	PLT    *sym.Symbol
	GOTPLT *sym.Symbol

	Tlsg      *sym.Symbol
	Tlsoffset int

	Dynamic *sym.Symbol
	DynSym  *sym.Symbol
	DynStr  *sym.Symbol

	// Elf specific
	Rel     *sym.Symbol
	Rela    *sym.Symbol
	RelPLT  *sym.Symbol
	RelaPLT *sym.Symbol

	// Darwin symbols
	LinkEditGOT *sym.Symbol
	LinkEditPLT *sym.Symbol

	// ----- loader.Sym equivalents -----

	TOC2    loader.Sym
	DotTOC2 []loader.Sym // for each version

	GOT2    loader.Sym
	PLT2    loader.Sym
	GOTPLT2 loader.Sym

	Tlsg2 loader.Sym

	Dynamic2 loader.Sym
	DynSym2  loader.Sym
	DynStr2  loader.Sym
}

// setArchSyms sets up the ArchSyms structure, and must be called before
// relocations are applied.
func (ctxt *Link) setArchSyms() {
	ctxt.GOT2 = ctxt.loader.LookupOrCreateSym(".got", 0)
	ctxt.GOT = ctxt.loader.Syms[ctxt.GOT2]
	ctxt.PLT2 = ctxt.loader.LookupOrCreateSym(".plt", 0)
	ctxt.PLT = ctxt.loader.Syms[ctxt.PLT2]
	ctxt.GOTPLT2 = ctxt.loader.LookupOrCreateSym(".got.plt", 0)
	ctxt.GOTPLT = ctxt.loader.Syms[ctxt.GOTPLT2]

	ctxt.Dynamic2 = ctxt.loader.LookupOrCreateSym(".dynamic", 0)
	ctxt.Dynamic = ctxt.loader.Syms[ctxt.Dynamic2]
	ctxt.DynSym2 = ctxt.loader.LookupOrCreateSym(".dynsym", 0)
	ctxt.DynSym = ctxt.loader.Syms[ctxt.DynSym2]
	ctxt.DynStr2 = ctxt.loader.LookupOrCreateSym(".dynstr", 0)
	ctxt.DynStr = ctxt.loader.Syms[ctxt.DynStr2]

	if ctxt.IsAIX() {
		ctxt.TOC2 = ctxt.loader.LookupOrCreateSym("TOC", 0)
		ctxt.TOC = ctxt.loader.Syms[ctxt.TOC2]
		ctxt.DotTOC = make([]*sym.Symbol, ctxt.Syms.MaxVersion()+1)
		ctxt.DotTOC2 = make([]loader.Sym, ctxt.Syms.MaxVersion()+1)
		for i := 0; i <= ctxt.Syms.MaxVersion(); i++ {
			if i >= 2 && i < sym.SymVerStatic { // these versions are not used currently
				continue
			}
			ctxt.DotTOC2[i] = ctxt.loader.LookupOrCreateSym(".TOC.", i)
			ctxt.DotTOC[i] = ctxt.loader.Syms[ctxt.DotTOC2[i]]
		}
	}
	if ctxt.IsElf() {
		ctxt.Rel = ctxt.Syms.Lookup(".rel", 0)
		ctxt.Rela = ctxt.Syms.Lookup(".rela", 0)
		ctxt.RelPLT = ctxt.Syms.Lookup(".rel.plt", 0)
		ctxt.RelaPLT = ctxt.Syms.Lookup(".rela.plt", 0)
	}
	if ctxt.IsDarwin() {
		ctxt.LinkEditGOT = ctxt.Syms.Lookup(".linkedit.got", 0)
		ctxt.LinkEditPLT = ctxt.Syms.Lookup(".linkedit.plt", 0)
	}
}

type Arch struct {
	Funcalign      int
	Maxalign       int
	Minalign       int
	Dwarfregsp     int
	Dwarfreglr     int
	Androiddynld   string
	Linuxdynld     string
{'fraction_non_alphanumeric': 0.11448973042362003, 'fraction_numerical': 0.02511232349165597, 'mean_word_length': 3.5894698085419736, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 25, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6696120', 'n_tokens_mistral': 4481, 'n_tokens_neox': 4105, 'n_words': 1389}