flat assembler
Message board for the users of flat assembler.

Index > Linux > reloc problem

Author
Thread Post new topic Reply to topic
nocona



Joined: 04 Aug 2007
Posts: 35
nocona
when assembling this (incomplete example of kernel module)

Code:
format elf64
extrn printk

section ".text" executable align 8
init_module:
    mov rdi, msg_init
    call printk
    xor eax, eax
    ret

cleanup_module:
    ret

;section ".gnu.linkonce.this_module" writeable align 64
;the "struct module" block
;qword at 0x178 is init_module entry
;qword at 0x298 is cleanup_module entry, if any
    


fasm generate E8 FC FF FF FF for the "call printk" code and generate a .rela.text section to fix the code. But from what i understand, rela relocation have explicit addend in the reloc info and the value at the location to be fixed is overwritten by the calculation of the relocation type (whatever it is), not being added to. so when this module is loaded (after linking the object file, of course), the call will not call printk (miss by 4 byte offset?) and cause error. fasm should generate E8 00 00 00 00 and put the -4 (qword) as the addend instead.

as a note, i had to do this to call the function:
Code:
    ...
    call qword [_printk]
    ...
_printk dq printk
    
Post 04 Aug 2007, 05:51
View user's profile Send private message Reply with quote
Dex4u



Joined: 08 Feb 2005
Posts: 1601
Location: web
Dex4u
Post 04 Aug 2007, 14:38
View user's profile Send private message Reply with quote
nocona



Joined: 04 Aug 2007
Posts: 35
nocona
afaik kernel know nothing about plt and got. it has its own symbols exporting mechanism and we just need to supply the rela relocation info with the accompanying symbol name.
Post 05 Aug 2007, 05:11
View user's profile Send private message Reply with quote
nocona



Joined: 04 Aug 2007
Posts: 35
nocona
i download version 1.67.22 but the problem still occurs. so here's my patch for this problem..it just patch one file - formats.inc and fix rela section reloc entry generation. hopefully Tomasz can check it.

Code:
diff -uprN fasm/source/formats.inc fasm-1.67.22/source/formats.inc
--- fasm/source/formats.inc      2007-08-06 10:32:50.000000000 +0800
+++ fasm-1.67.22/source/formats.inc      2007-08-14 14:44:55.000000000 +0800
@@ -1,4 +1,3 @@
-
 ; flat assembler core
 ; Copyright (c) 1999-2007, Tomasz Grysztar.
 ; All rights reserved.
@@ -3304,6 +3303,9 @@ elf_formatter:
        add     esi,20h
     xchg    edi,[esp]
   mov     ebp,edi
+    ;added
+     push    esi
+        ;added
       convert_relocations:
   cmp     esi,[free_additional_memory]
        je      relocations_converted
@@ -3336,9 +3338,35 @@ elf_formatter:
  mov     eax,[eax]
   shr     eax,8
       stos    dword [edi]
-        xor     eax,eax
-    stos    dword [edi]
-        stos    dword [edi]
+        ;removed
+   ;xor    eax,eax
+    ;stos   dword [edi]
+        ;stos   dword [edi]
+        ;removed
+   ;added
+     ;int3
+      push    ebx ecx
+    mov     ecx, [esp+8]
+       mov     ebx, [ecx-20h+8]
+   add     ebx, [esi+4]
+       xor     eax, eax
+   xchg    eax, [ebx]
+ stosd
+      movzx   eax, byte [esi]
+    cmp     al, 2   ;R_X86_64_PC32
+     je      .word32
+    cmp     al, 11  ;R_X86_64_32S
+      je      .word32
+    cmp     al, 4   ;R_X86_64_PLT32
+    je      .word32
+    xor     eax, eax
+   xchg    eax, [ebx+4]
+       stosd
+      jmp     .done
+      .word32:
+       xor     eax, eax
+   stosd
+      .done:
+ pop     ecx ebx
+    ;added
       relocation_entry_ok:
   add     esi,0Ch
     jmp     convert_relocations
@@ -3351,6 +3379,9 @@ elf_formatter:
       elf_machine_word_ok:
  ret
       relocations_converted:
+   ;added
+     add     esp, 4
+     ;added
      cmp     edi,ebp
     xchg    edi,[esp]
   je      rel_section_ok
Files fasm/source/Linux/fasm and fasm-1.67.22/source/Linux/fasm differ

    
Post 18 Aug 2007, 04:15
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.