This commit is contained in:
parent
ce5416bf91
commit
abbc4d9ae2
3 changed files with 29 additions and 21 deletions
|
@ -1,19 +1,15 @@
|
|||
package elf
|
||||
|
||||
import "bytes"
|
||||
import "strings"
|
||||
|
||||
// AddSections adds section headers to the ELF file.
|
||||
func (elf *ELF) AddSections() {
|
||||
elf.StringTable = []byte("\000.text\000.shstrtab\000")
|
||||
stringTableStart := elf.DataHeader.Offset + elf.DataHeader.SizeInFile
|
||||
sectionHeaderStart := stringTableStart + int64(len(elf.StringTable))
|
||||
|
||||
elf.SectionHeaders = []SectionHeader{
|
||||
{
|
||||
Type: SectionTypeNULL,
|
||||
},
|
||||
{
|
||||
NameIndex: int32(bytes.Index(elf.StringTable, []byte(".text\000"))),
|
||||
NameIndex: int32(strings.Index(StringTable, ".text\000")),
|
||||
Type: SectionTypePROGBITS,
|
||||
Flags: SectionFlagsAllocate | SectionFlagsExecutable,
|
||||
VirtualAddress: elf.CodeHeader.VirtualAddress,
|
||||
|
@ -22,16 +18,25 @@ func (elf *ELF) AddSections() {
|
|||
Align: elf.CodeHeader.Align,
|
||||
},
|
||||
{
|
||||
NameIndex: int32(bytes.Index(elf.StringTable, []byte(".shstrtab\000"))),
|
||||
NameIndex: int32(strings.Index(StringTable, ".rodata\000")),
|
||||
Type: SectionTypePROGBITS,
|
||||
Flags: SectionFlagsAllocate,
|
||||
VirtualAddress: elf.DataHeader.VirtualAddress,
|
||||
Offset: elf.DataHeader.Offset,
|
||||
SizeInFile: elf.DataHeader.SizeInFile,
|
||||
Align: elf.DataHeader.Align,
|
||||
},
|
||||
{
|
||||
NameIndex: int32(strings.Index(StringTable, ".shstrtab\000")),
|
||||
Type: SectionTypeSTRTAB,
|
||||
Offset: int64(stringTableStart),
|
||||
SizeInFile: int64(len(elf.StringTable)),
|
||||
Offset: int64(StringTableStart),
|
||||
SizeInFile: int64(len(StringTable)),
|
||||
Align: 1,
|
||||
},
|
||||
}
|
||||
|
||||
elf.SectionHeaderEntrySize = SectionHeaderSize
|
||||
elf.SectionHeaderEntryCount = int16(len(elf.SectionHeaders))
|
||||
elf.SectionHeaderOffset = int64(sectionHeaderStart)
|
||||
elf.SectionNameStringTableIndex = 2
|
||||
elf.SectionHeaderOffset = int64(SectionHeaderStart)
|
||||
elf.SectionNameStringTableIndex = int16(len(elf.SectionHeaders) - 1)
|
||||
}
|
|
@ -7,5 +7,9 @@ const (
|
|||
ArchitectureAMD64 = 0x3E
|
||||
ArchitectureARM64 = 0xB7
|
||||
ArchitectureRISCV = 0xF3
|
||||
HeaderEnd = HeaderSize + ProgramHeaderSize*2
|
||||
StringTable = "\000.text\000.rodata\000.shstrtab\000"
|
||||
StringTableStart = ProgramHeaderEnd
|
||||
SectionHeaderStart = StringTableStart + len(StringTable)
|
||||
ProgramHeaderEnd = HeaderSize + ProgramHeaderSize*2
|
||||
HeaderEnd = ProgramHeaderEnd + len(StringTable) + 4*SectionHeaderSize
|
||||
)
|
|
@ -14,7 +14,6 @@ type ELF struct {
|
|||
CodeHeader ProgramHeader
|
||||
DataHeader ProgramHeader
|
||||
SectionHeaders []SectionHeader
|
||||
StringTable []byte
|
||||
}
|
||||
|
||||
// Write writes the ELF64 format to the given writer.
|
||||
|
@ -70,10 +69,10 @@ func Write(writer io.WriteSeeker, b *build.Build, codeBytes []byte, dataBytes []
|
|||
binary.Write(writer, binary.LittleEndian, &elf.Header)
|
||||
binary.Write(writer, binary.LittleEndian, &elf.CodeHeader)
|
||||
binary.Write(writer, binary.LittleEndian, &elf.DataHeader)
|
||||
writer.Write([]byte(StringTable))
|
||||
binary.Write(writer, binary.LittleEndian, &elf.SectionHeaders)
|
||||
writer.Seek(int64(code.Padding), io.SeekCurrent)
|
||||
writer.Write(code.Bytes)
|
||||
writer.Seek(int64(data.Padding), io.SeekCurrent)
|
||||
writer.Write(data.Bytes)
|
||||
writer.Write(elf.StringTable)
|
||||
binary.Write(writer, binary.LittleEndian, &elf.SectionHeaders)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue