ld linker script, mark section RW

0

I do not understand why my linker script is not working as expected, I am compiling my assembly code using

nasm -f elf64 file.asm 

and later I link it using my custom linker script

ld -o file.elf64 -T linker_script.lds file.o

without linker script I can execute it, but with the linker script not, I just wanna place the text and code section on different locations and mark text as RWX and the data as RW.

MEMORY
{
  cod (RWX)  : ORIGIN = 0x0041000 , LENGTH = 0x1000
  mem (RW)  : ORIGIN = 0x0040000 , LENGTH = 0x1000
}

SECTIONS
{
  .data : { *(.data) } >mem
  .text : { *(.text) } >cod
}

If I view the Sections in r2 I get

r2 -c 'iS' -A file.elf64
[Sections]
Nm Paddr       Size Vaddr      Memsz Perms Name
00 0x00000000     0 0x00000000     0 ---- 
01 0x00001000    69 0x00041000    69 -r-- .TEXT
02 0x00001045    13 0x00041045    13 -r-- .DATA
03 0x00001058   168 0x00000000   168 ---- .symtab
04 0x00001100    33 0x00000000    33 ---- .strtab
05 0x00001121    39 0x00000000    39 ---- .shstrtab

which I do not understand

(For completness the asm-Code)

SECTION .TEXT
  GLOBAL _start 

_start:

  mov rax, 0 ; read syscall
  mov rdi, 0
  mov rsi, hello
  mov rdx, 10
  syscall

  mov rax, 1 ; write syscall
  mov rdi, 1
  mov rsi, hello
  mov rdx, 10
  syscall

  mov rax, 1
  syscall

SECTION .DATA
  hello:     db 'Hello world!',10   
  helloLen:  equ $-hello
assembly
linker
ld
asked on Stack Overflow Mar 21, 2019 by niku

1 Answer

2

Your problem is that you named .text and .data in lower case in your linker script but in upper case in your source file. Section names are case sensitive, so the linker doesn't recognise .DATA as the .data you specify in your linker script and doesn't do what you expect to do.

To fix this issue, consistently use the same case for section names.

Lastly, note that all section names beginning with a period (.) are reserved by the ELF standard for various purposes. If you ever want to add custom sections to your program, give them names that don't start with periods.

answered on Stack Overflow Mar 21, 2019 by fuz

User contributions licensed under CC BY-SA 3.0