I am using g++ on ubuntu. I compile some code and I need to unify the code section ".text" with the data section ".data" regarding a specific compilation unit ( object file ).

I already managed using directives like attribute((section(".mydata"))) to get both the compiled code and the global data defined into the same section, and I checked it with objdump.

I also managed to get it compiled, but afterwards a segment fault is issued when code is executed.

The problem is that this section should have execute write and read permissions, instead of "read and execute" or "read and write" that have the ".text" and ".data" sections respectively.

I suppose that using the attribute((section(".tdata",someflags))) this should be possible, but until now I didn't find anything.

... update ... I managed using the script bellow to create an object file as requested.

The main code is:

#include <stdio.h>

__attribute__((section(".napl2")))
char s[]="testString";

__attribute__((section(".napl1")))
void nothingToDo(){
    printf("%s","doneNothing\n");
}

int main()
{ printf("%s\n",s);
    nothingToDo();
    return 0;
}

Now I have the link script file bellow that is named "ldscript":

MEMORY {
  ERW_MEMORY(RWXA) : ORIGIN = 0xC000, LENGTH = 0x2000
}

SECTIONS{ 
  .napl : { 
        . = ALIGN(.);
    *(.napl1)
    *(.napl2)
    } > ERW_MEMORY
}

I compile using:

g++ -o Debug/test_object_file_section  -T ldscript main.cpp 

I get:

/usr/bin/ld: warning: Debug/test_object_file_section has a LOAD segment with RWX permissions

doing

objdump -d -j .napl Debug/test_object_file_section 

I get:

Debug/test_object_file_section: file format elf64-x86-64

Disassembly of section .napl:

000000000000c040 <_Z11nothingToDov>: c040: 55 push %rbp c041: 48 89 e5 mov %rsp,%rbp c044: bf 3c c4 00 00 mov $0xc43c,%edi c049: e8 52 01 00 00 call c1a0 puts@plt c04e: 90 nop c04f: 5d pop %rbp c050: c3 ret c051: 0f 1f 80 00 00 00 00 nopl 0x0(%rax)

000000000000c058 : c058: 74 65 73 74 53 74 72 69 6e 67 00 testString.

But I cannot still run the program as I get a

(segmentation fault)

Any ideas ?

0

There are 0 best solutions below