bug-binutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug ld/25207] ld: support --image-base= for elf (and -Ttext-segment -z


From: hakancandar at protonmail dot com
Subject: [Bug ld/25207] ld: support --image-base= for elf (and -Ttext-segment -z separate-code strangeness)
Date: Mon, 21 Oct 2024 22:45:23 +0000

https://sourceware.org/bugzilla/show_bug.cgi?id=25207

Hakan <hakancandar at protonmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hakancandar at protonmail dot 
com

--- Comment #1 from Hakan <hakancandar at protonmail dot com> ---
It has been five years since the initial bug report was filed, and I just
reconfirmed with the latest linker that the reported -Ttext-segment behaviour
when used with -z separate-code remains the same.

Effectively, -Ttext-segment historically and currently stands for specifying
the base address of the ELF file - not the first executable segment. Changing
the current behaviour of the the option has a high chance of breaking existing
projects that use this flag for specifying the base address, so in my opinion
it is more clever to preserve it.

The LLVM linker has dropped support for -Ttext-segment, and instead suggests
specifying --image-base with the following:
~ > ld.lld code.o -Ttext-segment=0x80000
ld.lld: error: -Ttext-segment is not supported. Use --image-base if you intend
to set the base address

All things considered, I suggest preserving the current behaviour of
-Ttext-segment for maintaining compatibility with old projects and introducing
a new option --image-base for compatibility with the LLVM linker.

Here is the patch that introduces the new option --image-base as described. I
also added a test case to ensure -Ttext-segment behaviour stays the same.

diff --git a/ld/NEWS b/ld/NEWS
index 1f14dd6bc77..e9ced61c1db 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -2,6 +2,9 @@

 Changes in 2.44:

+* Add --image-base=<ADDR> option to behave the same as -Ttext-segment
+  for compatibility with LLD.
+
 * Add a "--build-id=xx" option, if built with the xxhash library.  This
   produces a 128-bit hash, 2-4x faster than md5 or sha1.

diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em
index 2e865728587..327f95d62e5 100644
--- a/ld/emultempl/elf.em
+++ b/ld/emultempl/elf.em
@@ -817,6 +817,7 @@ fragment <<EOF
     {"compress-debug-sections", required_argument, NULL,
OPTION_COMPRESS_DEBUG},
     {"rosegment", no_argument, NULL, OPTION_ROSEGMENT},
     {"no-rosegment", no_argument, NULL, OPTION_NO_ROSEGMENT},
+    {"image-base", required_argument, NULL, OPTION_IMAGE_BASE},
 EOF
 if test x"$GENERATE_SHLIB_SCRIPT" = xyes; then
 fragment <<EOF
diff --git a/ld/ld.texi b/ld/ld.texi
index 90182c436ec..f51227b4b0f 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -2746,11 +2746,17 @@ Same as @option{--section-start}, with @code{.bss},
@code{.data} or
 @item -Ttext-segment=@var{org}
 @cindex text segment origin, cmd line
 When creating an ELF executable, it will set the address of the first
-byte of the text segment.  Note that when @option{-pie} is used with
+byte of the first segment.  Note that when @option{-pie} is used with
 @option{-Ttext-segment=@var{org}}, the output executable is marked
 ET_EXEC so that the address of the first byte of the text segment will
 be guaranteed to be @var{org} at run time.

+@kindex --image-base=@var{org}
+@item --image-base=@var{org}
+@cindex image base address, cmd line
+Same as @option{-Ttext-segment}, with both options effectively setting
+the base address of the ELF executable.
+
 @kindex -Trodata-segment=@var{org}
 @item -Trodata-segment=@var{org}
 @cindex rodata segment origin, cmd line
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 8982073bc91..887bede2a79 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -1478,6 +1478,8 @@ parse_args (unsigned argc, char **argv)
        case OPTION_TTEXT:
          set_segment_start (".text", optarg);
          break;
+       case OPTION_IMAGE_BASE:
+         /* --image-base and -Ttext-segment behavior is the same */
        case OPTION_TTEXT_SEGMENT:
          set_segment_start (".text-segment", optarg);
          break;
diff --git a/ld/testsuite/ld-elf/pr25207.d b/ld/testsuite/ld-elf/pr25207.d
new file mode 100644
index 00000000000..6b9965ab591
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr25207.d
@@ -0,0 +1,11 @@
+#source: pr25207.s
+#ld: -z separate-code -Ttext-segment=0x120000
+#readelf: -l --wide
+#target: *-*-linux* *-*-gnu* arm*-*-uclinuxfdpiceabi
+# changing -Ttext-segment behaviour will break --image-base (pr25207)
+# -Ttext-segment=<ADDR> should set the first segment address,
+# not necessarily the first executable segment.
+
+#...
+  LOAD +0x0+ 0x0*120000 0x0*120000 0x0*[0-9a-f][0-9a-f][0-9a-f]
0x0*[0-9a-f][0-9a-f][0-9a-f] R   .*
+#pass
diff --git a/ld/testsuite/ld-elf/pr25207.s b/ld/testsuite/ld-elf/pr25207.s
new file mode 100644
index 00000000000..ffa11bbc550
--- /dev/null
+++ b/ld/testsuite/ld-elf/pr25207.s
@@ -0,0 +1,8 @@
+        .section .text, "ax"
+       .globl  _start
+_start:
+       .space 1
+
+       .section .rodata
+       .globl  foo
+foo:   .space 1

-- 
You are receiving this mail because:
You are on the CC list for the bug.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]