How to prevent ld from combining writable and executable sections?

1

When I link together object files, the resulting ELF executable has (only) the following LOAD segment:

LOAD off    0x00000000 vaddr 0x00008000 paddr 0x00008000 align 2**15
     filesz 0x000010f0 memsz 0x000010f0 flags rwx

The linker ld combined all sections into one rwx segment, instead of separating writable and executable parts. I want to prevent this. The relocatable objects have the sections marked as writable or executable as appropriate, so this problem appears at the link time.

So, what determines how ld assigns permissions to the segments? The linker script does not seem to have anything related to that. Is it something specified when the toolchain is built?

I am targeting the ARM, and the toolchain is arm-linux-gnueabi, binutils version 2.22.

Edit: The linker script is here. The other linker options are -Bdynamic, --gc-sections, -z nocopyreloc and --no-undefined.

linker
arm
elf
asked on Stack Overflow Jun 4, 2012 by Juho Östman • edited Jun 7, 2012 by Juho Östman

1 Answer

1

Usually linker scripts are used to define sections, their order, whether to keep them or trash them from the binary (release build linker script might chose to remove debugging info).

Here is an example of a linker script: system-onesegment.ld

EDIT: to modify section permission, put this before whatever section you want to change the permission of .section .section_name, "permission".

Example:

.text
mov r0, r0
b main
.section .rodata, "ro"
.word 0x00000001
.previous
main: mov r0, r0
answered on Stack Overflow Jun 4, 2012 by sgupta • edited Jun 4, 2012 by sgupta

User contributions licensed under CC BY-SA 3.0