[klibc] [PATCH] support llvm's lld

Stanislav Fomichev sdf at google.com
Mon Jan 27 14:05:42 PST 2020


When using clang+llvm and lld, I get the following error:

ld.lld: error: -Ttext-segment is not supported. Use --image-base if you intend to set the base address

Let's test whether linker is lld and use --image-base instead of
-Ttext-segment. See llvm's rationale for not supporting -Ttext-segment:
https://github.com/llvm/llvm-project/commit/4dc2fb123dcfe9a97ad6f3a1135053b74efd0bc9

Note that I've tested x86_64 only, everything else is just a copy-paste.

Signed-off-by: Stanislav Fomichev <sdf at google.com>
---
 Makefile                       | 7 +++++++
 usr/klibc/arch/alpha/MCONFIG   | 2 +-
 usr/klibc/arch/arm/MCONFIG     | 4 ++--
 usr/klibc/arch/arm64/MCONFIG   | 2 +-
 usr/klibc/arch/i386/MCONFIG    | 2 +-
 usr/klibc/arch/m68k/MCONFIG    | 2 +-
 usr/klibc/arch/mips/MCONFIG    | 2 +-
 usr/klibc/arch/mips64/MCONFIG  | 2 +-
 usr/klibc/arch/parisc/MCONFIG  | 2 +-
 usr/klibc/arch/ppc/MCONFIG     | 2 +-
 usr/klibc/arch/ppc64/MCONFIG   | 2 +-
 usr/klibc/arch/riscv64/MCONFIG | 2 +-
 usr/klibc/arch/s390/MCONFIG    | 2 +-
 usr/klibc/arch/sh/MCONFIG      | 2 +-
 usr/klibc/arch/sparc/MCONFIG   | 2 +-
 usr/klibc/arch/sparc64/MCONFIG | 2 +-
 usr/klibc/arch/x86_64/MCONFIG  | 2 +-
 17 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/Makefile b/Makefile
index c99b962fd2a3..924c9fbe8de4 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,13 @@ export NM	:= $(KLIBCROSS)nm
 export OBJCOPY  := $(KLIBCROSS)objcopy
 export OBJDUMP  := $(KLIBCROSS)objdump
 
+LLD := $(shell $(LD) --version 2>&1 | grep -q LLD && echo true || echo false)
+ifeq ($(LLD),true)
+	export IMAGE_BASE=--image-base
+else
+	export IMAGE_BASE=-Ttext-segment
+endif
+
 NOSTDINC_FLAGS := -nostdlib -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 
 ARCH	          := $(shell uname -m | sed -e s/i.86/i386/ \
diff --git a/usr/klibc/arch/alpha/MCONFIG b/usr/klibc/arch/alpha/MCONFIG
index ad11372f9266..be56a0d52d4c 100644
--- a/usr/klibc/arch/alpha/MCONFIG
+++ b/usr/klibc/arch/alpha/MCONFIG
@@ -13,4 +13,4 @@ KLIBCBITSIZE  = 64
 # calls, and work on the memory models for this architecture
 # 7 GB - normal binaries start at 4.5 GB, and the stack is below
 # the binary.
-KLIBCSHAREDFLAGS	= -Ttext-segment 0x1c0000000
+KLIBCSHAREDFLAGS	= $(IMAGE_BASE) 0x1c0000000
diff --git a/usr/klibc/arch/arm/MCONFIG b/usr/klibc/arch/arm/MCONFIG
index 8a7096b8cced..34a3305da878 100644
--- a/usr/klibc/arch/arm/MCONFIG
+++ b/usr/klibc/arch/arm/MCONFIG
@@ -21,12 +21,12 @@ KLIBCREQFLAGS += -mthumb
 KLIBCLDFLAGS  += --thumb-entry _start
 KLIBCEMAIN     = --thumb-entry main
 KLIBCREQFLAGS += -mabi=aapcs-linux
-KLIBCSHAREDFLAGS = -Ttext-segment 0x380000
+KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x380000
 else
 # Extra linkflags when building the shared version of the library
 # This address needs to be reachable using normal inter-module
 # calls, and work on the memory models for this architecture
-KLIBCSHAREDFLAGS = -Ttext-segment 0x01800000
+KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x01800000
 ifeq ($(CONFIG_AEABI),y)
 KLIBCREQFLAGS += -mabi=aapcs-linux -mno-thumb-interwork
 else
diff --git a/usr/klibc/arch/arm64/MCONFIG b/usr/klibc/arch/arm64/MCONFIG
index 6d22847e670a..7ab22282ebbe 100644
--- a/usr/klibc/arch/arm64/MCONFIG
+++ b/usr/klibc/arch/arm64/MCONFIG
@@ -20,4 +20,4 @@ KLIBCREQFLAGS += -fno-exceptions -mgeneral-regs-only
 
 # On arm64, binaries are normally loaded at 4MB. Place klibc.so
 # a little before that at 2MB to prevent overlap.
-KLIBCSHAREDFLAGS = -Ttext-segment 0x0200000
+KLIBCSHAREDFLAGS = $(IMAGE_BASE) 0x0200000
diff --git a/usr/klibc/arch/i386/MCONFIG b/usr/klibc/arch/i386/MCONFIG
index 0704669500f0..baa7e59854f8 100644
--- a/usr/klibc/arch/i386/MCONFIG
+++ b/usr/klibc/arch/i386/MCONFIG
@@ -30,4 +30,4 @@ KLIBCBITSIZE  = 32
 # This address needs to be reachable using normal inter-module
 # calls, and work on the memory models for this architecture
 # 96 MB - normal binaries start at 128 MB
-KLIBCSHAREDFLAGS	= -Ttext-segment 0x06000000
+KLIBCSHAREDFLAGS	= $(IMAGE_BASE) 0x06000000
diff --git a/usr/klibc/arch/m68k/MCONFIG b/usr/klibc/arch/m68k/MCONFIG
index 3f4bdae645e4..3b170a9e1b2e 100644
--- a/usr/klibc/arch/m68k/MCONFIG
+++ b/usr/klibc/arch/m68k/MCONFIG
@@ -16,4 +16,4 @@ KLIBCBITSIZE  = 32
 # 2816 MB - normal binaries start at 2048 MB if I read the link
 # script right.  Not sure if there is a fundamental reason
 # to not duck below the halfway point...
-KLIBCSHAREDFLAGS        = -Ttext-segment 0xb0000000
+KLIBCSHAREDFLAGS        = $(IMAGE_BASE) 0xb0000000
diff --git a/usr/klibc/arch/mips/MCONFIG b/usr/klibc/arch/mips/MCONFIG
index 7ad652323362..61a3ea043cbe 100644
--- a/usr/klibc/arch/mips/MCONFIG
+++ b/usr/klibc/arch/mips/MCONFIG
@@ -17,4 +17,4 @@ KLIBCBITSIZE      = 32
 # 2 MB - normal binaries start at 4 MB
 #
 # Use -Ttext-segment so that the special .MIPS* sections are moved as well.
-KLIBCSHAREDFLAGS     = -Ttext-segment 0x00200000
+KLIBCSHAREDFLAGS     = $(IMAGE_BASE) 0x00200000
diff --git a/usr/klibc/arch/mips64/MCONFIG b/usr/klibc/arch/mips64/MCONFIG
index 6a4b41b2f85c..3d7f20da08be 100644
--- a/usr/klibc/arch/mips64/MCONFIG
+++ b/usr/klibc/arch/mips64/MCONFIG
@@ -20,4 +20,4 @@ KLIBCBITSIZE  = 64
 # load address, use the very top of the 256M region (minus 2MB)
 #
 # Use -Ttext-segment so that the special .MIPS* sections are moved as well.
-KLIBCSHAREDFLAGS     = -Ttext-segment 0x12FE00000
+KLIBCSHAREDFLAGS     = $(IMAGE_BASE) 0x12FE00000
diff --git a/usr/klibc/arch/parisc/MCONFIG b/usr/klibc/arch/parisc/MCONFIG
index 3472df67572c..ae92068792d4 100644
--- a/usr/klibc/arch/parisc/MCONFIG
+++ b/usr/klibc/arch/parisc/MCONFIG
@@ -9,4 +9,4 @@
 
 KLIBCOPTFLAGS += -Os -fomit-frame-pointer
 KLIBCBITSIZE  = 32
-KLIBCSHAREDFLAGS	= -Ttext-segment 0x40001000
+KLIBCSHAREDFLAGS	= $(IMAGE_BASE) 0x40001000
diff --git a/usr/klibc/arch/ppc/MCONFIG b/usr/klibc/arch/ppc/MCONFIG
index 8220f6b93fa4..ec859e8941d9 100644
--- a/usr/klibc/arch/ppc/MCONFIG
+++ b/usr/klibc/arch/ppc/MCONFIG
@@ -20,7 +20,7 @@ KLIBCBITSIZE       = 32
 # calls, and work on the memory models for this architecture
 # 256-16 MB - normal binaries start at 256 MB, and jumps are limited
 # to +/- 16 MB
-KLIBCSHAREDFLAGS     = -Ttext-segment 0x0f800000
+KLIBCSHAREDFLAGS     = $(IMAGE_BASE) 0x0f800000
 
 # The asm include files live in asm-powerpc
 KLIBCASMARCH	= powerpc
diff --git a/usr/klibc/arch/ppc64/MCONFIG b/usr/klibc/arch/ppc64/MCONFIG
index 0315110ca379..442d452bf11b 100644
--- a/usr/klibc/arch/ppc64/MCONFIG
+++ b/usr/klibc/arch/ppc64/MCONFIG
@@ -18,7 +18,7 @@ KLIBCBITSIZE      = 64
 # calls, and work on the memory models for this architecture
 # 256-16 MB - normal binaries start at 256 MB, and jumps are limited
 # to +/- 16 MB
-KLIBCSHAREDFLAGS     = -Ttext-segment 0x0f000000
+KLIBCSHAREDFLAGS     = $(IMAGE_BASE) 0x0f000000
 
 # The asm include files live in asm-powerpc
 KLIBCASMARCH	= powerpc
diff --git a/usr/klibc/arch/riscv64/MCONFIG b/usr/klibc/arch/riscv64/MCONFIG
index 61681509d89d..2ea257548417 100644
--- a/usr/klibc/arch/riscv64/MCONFIG
+++ b/usr/klibc/arch/riscv64/MCONFIG
@@ -17,4 +17,4 @@ endif
 KLIBCBITSIZE      = 64
 
 # Normal binaries start at 64 KB, so start the libary at 2 MB.
-KLIBCSHAREDFLAGS  =-Ttext-segment 0x00200000
+KLIBCSHAREDFLAGS  = $(IMAGE_BASE) 0x00200000
diff --git a/usr/klibc/arch/s390/MCONFIG b/usr/klibc/arch/s390/MCONFIG
index 637f5aa67ba7..8e29a4e69e6c 100644
--- a/usr/klibc/arch/s390/MCONFIG
+++ b/usr/klibc/arch/s390/MCONFIG
@@ -20,4 +20,4 @@ else
 endif
 
 KLIBCASMARCH		= s390
-KLIBCSHAREDFLAGS	= -Ttext-segment 0x40000000
+KLIBCSHAREDFLAGS	= $(IMAGE_BASE) 0x40000000
diff --git a/usr/klibc/arch/sh/MCONFIG b/usr/klibc/arch/sh/MCONFIG
index 2facf7cb5d29..34825631b7b2 100644
--- a/usr/klibc/arch/sh/MCONFIG
+++ b/usr/klibc/arch/sh/MCONFIG
@@ -14,4 +14,4 @@ KLIBCBITSIZE      = 32
 # This address needs to be reachable using normal inter-module
 # calls, and work on the memory models for this architecture
 # 2 MB -- the normal starting point for text is 4 MB.
-KLIBCSHAREDFLAGS	= -Ttext-segment 0x00200000
+KLIBCSHAREDFLAGS	= $(IMAGE_BASE) 0x00200000
diff --git a/usr/klibc/arch/sparc/MCONFIG b/usr/klibc/arch/sparc/MCONFIG
index 0623fdc2824b..aa172f8a5555 100644
--- a/usr/klibc/arch/sparc/MCONFIG
+++ b/usr/klibc/arch/sparc/MCONFIG
@@ -16,4 +16,4 @@ KLIBCARCHREQFLAGS	+= -D__sparc32__
 # calls, and work on the memory models for this architecture
 # Normal binaries start at 64K; the linker wants 64K alignment,
 # and call instructions have a 30-bit signed offset, << 2.
-KLIBCSHAREDFLAGS	 = -Ttext-segment 0x40000000
+KLIBCSHAREDFLAGS	 = $(IMAGE_BASE) 0x40000000
diff --git a/usr/klibc/arch/sparc64/MCONFIG b/usr/klibc/arch/sparc64/MCONFIG
index c83398559f84..767232fce80e 100644
--- a/usr/klibc/arch/sparc64/MCONFIG
+++ b/usr/klibc/arch/sparc64/MCONFIG
@@ -18,4 +18,4 @@ KLIBCLDFLAGS      = -m elf64_sparc
 # calls, and work on the memory models for this architecture
 # Normal binaries start at 1 MB; the linker wants 1 MB alignment,
 # and call instructions have a 30-bit signed offset, << 2.
-KLIBCSHAREDFLAGS	= -Ttext-segment 0x80000000
+KLIBCSHAREDFLAGS	= $(IMAGE_BASE) 0x80000000
diff --git a/usr/klibc/arch/x86_64/MCONFIG b/usr/klibc/arch/x86_64/MCONFIG
index c5f2fa265a26..da1ad5d48fa1 100644
--- a/usr/klibc/arch/x86_64/MCONFIG
+++ b/usr/klibc/arch/x86_64/MCONFIG
@@ -35,4 +35,4 @@ KLIBCLDFLAGS      = -m elf_x86_64
 # The old default was max-page-size=0x100000, but that also results
 # in a broken layout with binutils 2.30.  Since there's no
 # architectural page size betwen 4 KB and 2MB, set it to 4 KB.
-KLIBCSHAREDFLAGS     = -Ttext-segment 0x00200000 -z max-page-size=0x1000
+KLIBCSHAREDFLAGS     = $(IMAGE_BASE) 0x00200000 -z max-page-size=0x1000
-- 
2.25.0.341.g760bfbb309-goog



More information about the klibc mailing list