Specify a minimum starting address for text segment

288 views Asked by At

How can I tell lld to place code at or above a certain position?

I have a legacy piece of code that relies on a rather questionable piece of logic: a certain function can receive a value that either represents an index into an array of function pointers, or actually is the function pointer. The function determines the value's type by checking if the value is less than 0x10000; if so, it's an index. If not, it's a function pointer.

Much as I would really like to, I cannot change this piece of code.

Unfortunately, the test doesn't even work: on OSX (it hasn't manifested as a problem for anyone yet elsewhere), function pointers in the program can have much lower addresses than that threshold value, meaning that the function in question detects them as indices rather than pointers and everything goes wrong.

Is there an instruction I can give the linker - lld in this case - to force it to place all generated code at or above 0x10000 so that this problem goes away until someone can fix the ridiculous test?

I tried the -sectalign option, but that gives the following error:

ld: argument for -sectalign must be less than or equal to 0x8000

0x8000 isn't enough; code is still placed in the space between it and 0x10000 and the logic still fails.

Is there another option I can use to do this?

1

There are 1 answers

0
Olsonist On

You will need to write a linker script. LLD supports GNU LD style linker scripts. They will look something like:

SECTIONS
{
  . = 0x10000;
  .text : { *(.text) }
  . = 0x8000000;
  .data : { *(.data) }
  .bss : { *(.bss) }
}

This script will place the .text section at 0x8000000.