2

I am trying to implement custom loader and

want to locate two program headers(segment) for data and code with 0x1000 aligned.

I fixed some part of the default linker script and get weird results.

**Default linker script.**
. = ALIGN (CONSTANT (MAXPAGESIZE)) - ((CONSTANT (MAXPAGESIZE) - .) & 
(CONSTANT (MAXPAGESIZE) - 1));
. = DATA_SEGMENT_ALIGN (CONSTANT(MAXPAGESIZE),CONSTANT (COMMONPAGESIZE));

**Modified linker script**
. = ALIGN (0x1000);
. = DATA_SEGMENT_ALIGN(0x1000, 0x1000);

when I compiled the binary with default linker script, it is 0x200000 aligned

and have two program headers.

LOAD         0x0000000000000000 0x0000000050000000 0x0000000050000000
             0x0000000000001058 0x0000000000001058  R E    200000
LOAD         0x0000000000001fe8 0x0000000050201fe8 0x0000000050201fe8
             0x0000000000000028 0x00000000000000c0  RW     200000

but I get below result with modified linker script.

LOAD           0x0000000000000000 0x0000000050000000 0x0000000050000000
               0x0000000000002010 0x00000000000020a8  RWE    200000

It seems that the data section and code section is mixed in one program header.

However, I want to make my program have two page aligned(0x1000) program headers

LOAD1        0x0000000050000000 ~ 0x0000000050002340 R E
LOAD2        0x0000000050003000 ~ 0x0000000050006790 RW 

Please let me know some directions.

ruach
  • 1,369
  • 11
  • 21
  • 1
    I haven't dealt much with linker scripts, but I expect the script does more than just set `.`. Is this snippet between the section declarations? Could you add more of the surrounding script? – Hasturkun Mar 16 '16 at 13:15
  • By default, ld linker script locates two segment with 0x200000 alignment (one for code(RX) and one for data(RW)), but what I want is locating two segment back to back. Briefly speaking, I don't want to make some space between two segment because I don't want to waste my memory pages. This is x64 binary and data is referenced using the offset, So I want my linker script locate these two segments back to back. Thanks – ruach Mar 16 '16 at 13:53
  • I found the answer about this question. Please look at this URL http://stackoverflow.com/questions/33005638/how-to-change-alignment-of-code-segment-in-elf – ruach Mar 17 '16 at 03:03
  • Try linking with the option `-z max-page-size=0x1000` which changes the default ma page size from 2mb to 4mb. – Michael Petch Feb 16 '18 at 22:14

0 Answers0