elf: get R_X86_64_64 relocations working correctly

This commit is contained in:
mappu 2023-12-11 19:00:08 +13:00
parent 082a1d4f9f
commit cc2671e208
1 changed files with 17 additions and 7 deletions

View File

@ -123,6 +123,7 @@ func (c *compiler) CreateSymbol(name string, class string, offset int64, length
// created, linking it with any other .o files will create a combined .text
// section where all the offsets have shifted
esym := Elf64_Sym{}
esym.St_value = uint64(offset)
if class == `.section` {
esym.St_name = uint32(c.StringTable(name)) // Write name into public string table
@ -231,18 +232,26 @@ func (c *compiler) Reloc(symbolName string, mode ElfRelocationType) error {
}
// Find the symbol pointing to its parent section
parentSectionSyminfo, ok := c.symtab[syminfo.sectionName]
if !ok {
return fmt.Errorf("Bad parent section")
}
/*
parentSectionSyminfo, ok := c.symtab[syminfo.sectionName]
if !ok {
return fmt.Errorf("Bad parent section")
}
fmt.Printf("-->Relocation %q found in %q (sectionidx %d)\n", symbolName, syminfo.sectionName, parentSectionSyminfo.symtabSectionIndex)
fmt.Printf("-->Relocation %q found in %q (sectionidx %d)\n", symbolName, syminfo.sectionName, parentSectionSyminfo.symtabSectionIndex)
rootSymbol := parentSectionSyminfo.symtabSectionIndex
if rootSymbol == 5 {
rootSymbol = 7
}
*/
rootSymbol := syminfo.symtabSectionIndex
// Add the relocation to the .rela section
rr := Elf64_Rela{}
rr.R_offset = uint64(c.currentSection.buff.Len())
rr.R_info = uint64(parentSectionSyminfo.symtabSectionIndex)<<32 | uint64(mode) // high bits: Index of search symbol in the symtab (the source section). low bits: mode type
rr.R_addend = syminfo.offset // Add to the result when relocating (offset within source section)
rr.R_info = uint64(rootSymbol)<<32 | uint64(mode) // high bits: Index of search symbol in the symtab (the source section). low bits: mode type
rr.R_addend = 0 // syminfo.offset // Add to the result when relocating (offset within source section)
err := binary.Write(relaSec.buff, binary.LittleEndian, &rr)
if err != nil {
@ -353,6 +362,7 @@ func (c *compiler) Compile(t Token) error {
// mov $var, rxx
// Load register's contents into variable
// x86_64 can only really do this in a single instruction with 32-bit displacement, not full 64-bit
// The PIC alternative is to transform this into `lea symbol(%rip), %rdi`
switch tok.Args[1] {
case "rax":