ld/ELF: Add --image-base command line option to the ELF linker

LLD has dropped the option -Ttext-segment for specifying image base
addresses, instead forcing the use of the --image-base option for both
ELF and PE targets. As it stands, GNU LD and LLVM LLD are incompatible,
having two different options for the same functionality.

This patch enables the use of --image-base on ELF targets, advancing
consistency and compatibility.

See: https://reviews.llvm.org/D70468
     https://maskray.me/blog/2020-11-15-explain-gnu-linker-options#address-related
     https://sourceware.org/bugzilla/show_bug.cgi?id=25207

Moreover, a new test has been added to ensure -z separate-code behaviour
when used with -Ttext-segment stays the same. When this combination is
used, -Ttext-segment sets the address of the first segment (R), not the
text segment (RX), and like with -z noseparate-code, no segments lesser
than the specified address are created. If this behaviour was to change,
the first (R) segment of the ELF file would begin in a lesser address
than the specified text (RX) segment, breaking traditional use of this
option for specifying image base address.
This commit is contained in:
Hakan Candar
2024-10-28 11:01:59 +00:00
committed by Alan Modra
parent 80ac478511
commit f4e363cae2
9 changed files with 47 additions and 16 deletions

View File

@@ -510,6 +510,8 @@ static const struct ld_option ld_options[] =
{ {"section-start", required_argument, NULL, OPTION_SECTION_START},
'\0', N_("SECTION=ADDRESS"), N_("Set address of named section"),
TWO_DASHES },
{ {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
'\0', N_("ADDRESS"), N_("Set image base address"), TWO_DASHES },
{ {"Tbss", required_argument, NULL, OPTION_TBSS},
'\0', N_("ADDRESS"), N_("Set address of .bss section"), ONE_DASH },
{ {"Tdata", required_argument, NULL, OPTION_TDATA},
@@ -1477,6 +1479,9 @@ parse_args (unsigned argc, char **argv)
case OPTION_TTEXT:
set_segment_start (".text", optarg);
break;
case OPTION_IMAGE_BASE:
/* Unless PE, --image-base and -Ttext-segment behavior is the same
PE-specific functionality is implemented in emultempl/{pe, pep, beos}.em */
case OPTION_TTEXT_SEGMENT:
set_segment_start (".text-segment", optarg);
break;