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

2024-10-21 Thread hakancandar at protonmail dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=25207

Hakan  changed:

   What|Removed |Added

 CC||hakancandar at protonmail dot 
com

--- Comment #1 from Hakan  ---
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=0x8
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= 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 < should set the first segment address,
+# not necessarily the first executable segment.
+
+#...
+  LOAD +0x0+ 0x0*12 0x0*12 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 000..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.


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

2024-10-29 Thread hakancandar at protonmail dot com
https://sourceware.org/bugzilla/show_bug.cgi?id=25207

--- Comment #2 from Hakan  ---
An updated patch has been pushed to main branch.

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