linker

package
v0.0.0-...-f3b96fd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 6, 2023 License: AGPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const ArHdrSize = unsafe.Sizeof(ArHdr{})

archive file header size

View Source
const EhdrSize = unsafe.Sizeof(EHdr{})

elf header size

View Source
const ElfObjectMagicNumber = "\177ELF"
View Source
const ElfStaticLinkMagicNumber = "!<arch>\n"
View Source
const SHdrSize = unsafe.Sizeof(SHdr{})

section header size

View Source
const SymSize = unsafe.Sizeof(Sym{})

符号表结构的大小

Variables

This section is empty.

Functions

func CheckMagic

func CheckMagic(contents []byte, magic string) bool

检查文件内容的前四个字符

func ElfGetName

func ElfGetName(strTab []byte, offset uint32) string

根据在shstrtab这个sh中的偏移量,返回相应sh的名字的字符串

func ReadFile

func ReadFile(ctx *Context, file *File)

func ReadInputFiles

func ReadInputFiles(ctx *Context, remaining []string)

处理remaining中要链接的库文件

Types

type ArHdr

type ArHdr struct {
	Name [16]byte
	Date [12]byte
	Uid  [6]byte
	Gid  [6]byte
	Mode [8]byte
	Size [10]byte // 保存一个section中数据区域的大小
	Fmag [2]byte
}

func (*ArHdr) GetSize

func (a *ArHdr) GetSize() int

返回archive中一个section的size

func (*ArHdr) HasPrefix

func (a *ArHdr) HasPrefix(s string) bool

func (*ArHdr) IsStrtab

func (a *ArHdr) IsStrtab() bool

string table

func (*ArHdr) IsSymtab

func (a *ArHdr) IsSymtab() bool

symbol table

func (*ArHdr) ReadName

func (a *ArHdr) ReadName(strTab []byte) string

type Context

type Context struct {
	Args ContextArgs
	Objs []*ObjFile
}

func NewContext

func NewContext() *Context

type ContextArgs

type ContextArgs struct {
	Output       string      // 保存链接器最后生成的文件名字
	Emulation    MachineType // 哪种架构
	LibraryPaths []string    // 库文件的路径,这是个数组
}

type EHdr

type EHdr struct {
	Ident     [16]uint8
	Type      uint16
	Machine   uint16
	Version   uint32
	Entry     uint64
	PhOff     uint64
	ShOff     uint64
	Flags     uint32
	EhSize    uint16
	PhEntSize uint16
	PhNum     uint16
	ShEntSize uint16
	ShNum     uint16
	ShStrNdx  uint16
}

elf header struct

type File

type File struct {
	Filename string
	Contents []byte
	Parent   *File
}

func FindLibraryByName

func FindLibraryByName(ctx *Context, name string) *File

根据静态链接库的名字,找到链接库文件

func MustNewFile

func MustNewFile(filename string) *File

这个函数必须正确返回

func OpenLibrary

func OpenLibrary(filepath string) *File

检验文件

func ReadArchiveMembers

func ReadArchiveMembers(file *File) []*File

type FileType

type FileType uint16
const (
	FileTypeUnknown FileType = iota
	FileTypeEmpty   FileType = iota
	FileTypeObject  FileType = iota // 对象文件   xxx.o
	FileTypeArchive FileType = iota // 静态链接库文件 xxx.a
)

func GetFileType

func GetFileType(contents []byte) FileType

首先判断是不是elf格式的文件, 然后判断elf header中的type字段

type InputFile

type InputFile struct {
	File         *File
	ElfSections  []SHdr // section数组
	ShStrtab     []byte // 对象文件的strtab sh
	FirstGlobal  int64
	ElfSyms      []Sym  // 符号表数组
	SymbolStrtab []byte // 符号表的str table
}

链接文件的基类

func NewInputFile

func NewInputFile(file *File) InputFile

func (*InputFile) FillUpElfSyms

func (f *InputFile) FillUpElfSyms(s *SHdr)

填充符号表数组 symbol table

func (*InputFile) FindfSection

func (f *InputFile) FindfSection(typ uint32) *SHdr

在对象文件的sh数组中,找到相应类型的sh

func (*InputFile) GetBytesFromIdx

func (f *InputFile) GetBytesFromIdx(idx int64) []byte

根据section header在数组中的偏移,拿到section的内容

func (*InputFile) GetBytesFromShdr

func (f *InputFile) GetBytesFromShdr(s *SHdr) []byte

根据secion header拿到对应的section的内容

type MachineType

type MachineType = uint8
const (
	MachineTypeNone    MachineType = iota
	MachinetypeRISCV64 MachineType = iota
)

func GetMachineTypeFromContents

func GetMachineTypeFromContents(contents []byte) MachineType

type MachineTypeStringer

type MachineTypeStringer struct {
	MachineType
}

func (MachineTypeStringer) String

func (m MachineTypeStringer) String() string

type ObjFile

type ObjFile struct {
	InputFile       // 结构体组合
	SymtabSec *SHdr // `symbol table`这个sh包含了所有sh的类型信息
}

func CreateObjectFile

func CreateObjectFile(file *File) *ObjFile

func NewObjFile

func NewObjFile(file *File) *ObjFile

func (*ObjFile) Parse

func (o *ObjFile) Parse()

type SHdr

type SHdr struct {
	Name      uint32 // 这个name是指再shstrtab这个sh中,当前sh名字的v起始偏移
	Type      uint32
	Flags     uint64
	Addr      uint64
	Offset    uint64
	Size      uint64
	Link      uint32
	Info      uint32
	AddrAlign uint64
	EntSize   uint64
}

section header struct

type Sym

type Sym struct {
	Name  uint32
	Info  uint8
	Other uint8
	Shndx uint16
	Val   uint64
	Size  uint64
}

符号表项

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL