Reorganized elf sections
All checks were successful
/ test (push) Successful in 15s

This commit is contained in:
Eduard Urbach 2025-06-27 13:15:11 +02:00
parent ce5416bf91
commit abbc4d9ae2
Signed by: akyoto
GPG key ID: 49226B848C78F6C8
3 changed files with 29 additions and 21 deletions

View file

@ -1,19 +1,15 @@
package elf package elf
import "bytes" import "strings"
// AddSections adds section headers to the ELF file. // AddSections adds section headers to the ELF file.
func (elf *ELF) AddSections() { 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{ elf.SectionHeaders = []SectionHeader{
{ {
Type: SectionTypeNULL, Type: SectionTypeNULL,
}, },
{ {
NameIndex: int32(bytes.Index(elf.StringTable, []byte(".text\000"))), NameIndex: int32(strings.Index(StringTable, ".text\000")),
Type: SectionTypePROGBITS, Type: SectionTypePROGBITS,
Flags: SectionFlagsAllocate | SectionFlagsExecutable, Flags: SectionFlagsAllocate | SectionFlagsExecutable,
VirtualAddress: elf.CodeHeader.VirtualAddress, VirtualAddress: elf.CodeHeader.VirtualAddress,
@ -22,16 +18,25 @@ func (elf *ELF) AddSections() {
Align: elf.CodeHeader.Align, 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, Type: SectionTypeSTRTAB,
Offset: int64(stringTableStart), Offset: int64(StringTableStart),
SizeInFile: int64(len(elf.StringTable)), SizeInFile: int64(len(StringTable)),
Align: 1, Align: 1,
}, },
} }
elf.SectionHeaderEntrySize = SectionHeaderSize elf.SectionHeaderEntrySize = SectionHeaderSize
elf.SectionHeaderEntryCount = int16(len(elf.SectionHeaders)) elf.SectionHeaderEntryCount = int16(len(elf.SectionHeaders))
elf.SectionHeaderOffset = int64(sectionHeaderStart) elf.SectionHeaderOffset = int64(SectionHeaderStart)
elf.SectionNameStringTableIndex = 2 elf.SectionNameStringTableIndex = int16(len(elf.SectionHeaders) - 1)
} }

View file

@ -1,11 +1,15 @@
package elf package elf
const ( const (
LittleEndian = 1 LittleEndian = 1
TypeExecutable = 2 TypeExecutable = 2
TypeDynamic = 3 TypeDynamic = 3
ArchitectureAMD64 = 0x3E ArchitectureAMD64 = 0x3E
ArchitectureARM64 = 0xB7 ArchitectureARM64 = 0xB7
ArchitectureRISCV = 0xF3 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
) )

View file

@ -14,7 +14,6 @@ type ELF struct {
CodeHeader ProgramHeader CodeHeader ProgramHeader
DataHeader ProgramHeader DataHeader ProgramHeader
SectionHeaders []SectionHeader SectionHeaders []SectionHeader
StringTable []byte
} }
// Write writes the ELF64 format to the given writer. // 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.Header)
binary.Write(writer, binary.LittleEndian, &elf.CodeHeader) binary.Write(writer, binary.LittleEndian, &elf.CodeHeader)
binary.Write(writer, binary.LittleEndian, &elf.DataHeader) 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.Seek(int64(code.Padding), io.SeekCurrent)
writer.Write(code.Bytes) writer.Write(code.Bytes)
writer.Seek(int64(data.Padding), io.SeekCurrent) writer.Seek(int64(data.Padding), io.SeekCurrent)
writer.Write(data.Bytes) writer.Write(data.Bytes)
writer.Write(elf.StringTable)
binary.Write(writer, binary.LittleEndian, &elf.SectionHeaders)
} }