Skip to content

Commit 00cda11

Browse files
sumanthkorikkarAlexander Gordeev
authored andcommitted
s390: Compile kernel with -fPIC and link with -no-pie
When the kernel is built with CONFIG_PIE_BUILD option enabled it uses dynamic symbols, for which the linker does not allow more than 64K number of entries. This can break features like kpatch. Hence, whenever possible the kernel is built with CONFIG_PIE_BUILD option disabled. For that support of unaligned symbols generated by linker scripts in the compiler is necessary. However, older compilers might lack such support. In that case the build process resorts to CONFIG_PIE_BUILD option-enabled build. Compile object files with -fPIC option and then link the kernel binary with -no-pie linker option. As result, the dynamic symbols are not generated and not only kpatch feature succeeds, but also the whole CONFIG_PIE_BUILD option-enabled code could be dropped. [ agordeev: Reworded the commit message ] Suggested-by: Ulrich Weigand <ulrich.weigand@de.ibm.com> Signed-off-by: Sumanth Korikkar <sumanthk@linux.ibm.com> Reviewed-by: Alexander Gordeev <agordeev@linux.ibm.com> Signed-off-by: Alexander Gordeev <agordeev@linux.ibm.com>
1 parent 5f90003 commit 00cda11

File tree

7 files changed

+3
-108
lines changed

7 files changed

+3
-108
lines changed

arch/s390/Kconfig

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -593,18 +593,6 @@ config RELOCATABLE
593593
Note: this option exists only for documentation purposes, please do
594594
not remove it.
595595

596-
config PIE_BUILD
597-
def_bool CC_IS_CLANG && !$(cc-option,-munaligned-symbols)
598-
help
599-
If the compiler is unable to generate code that can manage unaligned
600-
symbols, the kernel is linked as a position-independent executable
601-
(PIE) and includes dynamic relocations that are processed early
602-
during bootup.
603-
604-
For kpatch functionality, it is recommended to build the kernel
605-
without the PIE_BUILD option. PIE_BUILD is only enabled when the
606-
compiler lacks proper support for handling unaligned symbols.
607-
608596
config RANDOMIZE_BASE
609597
bool "Randomize the address of the kernel image (KASLR)"
610598
default y

arch/s390/Makefile

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,9 @@ KBUILD_AFLAGS_MODULE += -fPIC
1414
KBUILD_CFLAGS_MODULE += -fPIC
1515
KBUILD_AFLAGS += -m64
1616
KBUILD_CFLAGS += -m64
17-
ifdef CONFIG_PIE_BUILD
18-
KBUILD_CFLAGS += -fPIE
19-
LDFLAGS_vmlinux := -pie -z notext
20-
else
21-
KBUILD_CFLAGS += $(call cc-option,-munaligned-symbols,)
22-
LDFLAGS_vmlinux := --emit-relocs --discard-none
17+
KBUILD_CFLAGS += -fPIC
18+
LDFLAGS_vmlinux := -no-pie --emit-relocs --discard-none
2319
extra_tools := relocs
24-
endif
2520
aflags_dwarf := -Wa,-gdwarf-2
2621
KBUILD_AFLAGS_DECOMPRESSOR := $(CLANG_FLAGS) -m64 -D__ASSEMBLY__
2722
ifndef CONFIG_AS_IS_LLVM

arch/s390/boot/Makefile

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ CFLAGS_sclp_early_core.o += -I$(srctree)/drivers/s390/char
3737

3838
obj-y := head.o als.o startup.o physmem_info.o ipl_parm.o ipl_report.o vmem.o
3939
obj-y += string.o ebcdic.o sclp_early_core.o mem.o ipl_vmparm.o cmdline.o
40-
obj-y += version.o pgm_check_info.o ctype.o ipl_data.o
41-
obj-y += $(if $(CONFIG_PIE_BUILD),machine_kexec_reloc.o,relocs.o)
40+
obj-y += version.o pgm_check_info.o ctype.o ipl_data.o relocs.o
4241
obj-$(findstring y, $(CONFIG_PROTECTED_VIRTUALIZATION_GUEST) $(CONFIG_PGSTE)) += uv.o
4342
obj-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
4443
obj-y += $(if $(CONFIG_KERNEL_UNCOMPRESSED),,decompressor.o) info.o
@@ -49,9 +48,7 @@ targets := bzImage section_cmp.boot.data section_cmp.boot.preserved.data $(obj-y
4948
targets += vmlinux.lds vmlinux vmlinux.bin vmlinux.bin.gz vmlinux.bin.bz2
5049
targets += vmlinux.bin.xz vmlinux.bin.lzma vmlinux.bin.lzo vmlinux.bin.lz4
5150
targets += vmlinux.bin.zst info.bin syms.bin vmlinux.syms $(obj-all)
52-
ifndef CONFIG_PIE_BUILD
5351
targets += relocs.S
54-
endif
5552

5653
OBJECTS := $(addprefix $(obj)/,$(obj-y))
5754
OBJECTS_ALL := $(addprefix $(obj)/,$(obj-all))
@@ -110,13 +107,11 @@ OBJCOPYFLAGS_vmlinux.bin := -O binary --remove-section=.comment --remove-section
110107
$(obj)/vmlinux.bin: vmlinux FORCE
111108
$(call if_changed,objcopy)
112109

113-
ifndef CONFIG_PIE_BUILD
114110
CMD_RELOCS=arch/s390/tools/relocs
115111
quiet_cmd_relocs = RELOCS $@
116112
cmd_relocs = $(CMD_RELOCS) $< > $@
117113
$(obj)/relocs.S: vmlinux FORCE
118114
$(call if_changed,relocs)
119-
endif
120115

121116
suffix-$(CONFIG_KERNEL_GZIP) := .gz
122117
suffix-$(CONFIG_KERNEL_BZIP2) := .bz2

arch/s390/boot/boot.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ struct vmlinux_info {
2424
unsigned long bootdata_size;
2525
unsigned long bootdata_preserved_off;
2626
unsigned long bootdata_preserved_size;
27-
#ifdef CONFIG_PIE_BUILD
28-
unsigned long dynsym_start;
29-
unsigned long rela_dyn_start;
30-
unsigned long rela_dyn_end;
31-
#else
3227
unsigned long got_start;
3328
unsigned long got_end;
34-
#endif
3529
unsigned long amode31_size;
3630
unsigned long init_mm_off;
3731
unsigned long swapper_pg_dir_off;

arch/s390/boot/startup.c

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -151,41 +151,6 @@ static void copy_bootdata(void)
151151
memcpy((void *)vmlinux.bootdata_preserved_off, __boot_data_preserved_start, vmlinux.bootdata_preserved_size);
152152
}
153153

154-
#ifdef CONFIG_PIE_BUILD
155-
static void kaslr_adjust_relocs(unsigned long min_addr, unsigned long max_addr,
156-
unsigned long offset, unsigned long phys_offset)
157-
{
158-
Elf64_Rela *rela_start, *rela_end, *rela;
159-
int r_type, r_sym, rc;
160-
Elf64_Addr loc, val;
161-
Elf64_Sym *dynsym;
162-
163-
rela_start = (Elf64_Rela *) vmlinux.rela_dyn_start;
164-
rela_end = (Elf64_Rela *) vmlinux.rela_dyn_end;
165-
dynsym = (Elf64_Sym *) vmlinux.dynsym_start;
166-
for (rela = rela_start; rela < rela_end; rela++) {
167-
loc = rela->r_offset + phys_offset - __START_KERNEL;
168-
val = rela->r_addend;
169-
r_sym = ELF64_R_SYM(rela->r_info);
170-
if (r_sym) {
171-
if (dynsym[r_sym].st_shndx != SHN_UNDEF)
172-
val += dynsym[r_sym].st_value + offset - __START_KERNEL;
173-
} else {
174-
/*
175-
* 0 == undefined symbol table index (SHN_UNDEF),
176-
* used for R_390_RELATIVE, only add KASLR offset
177-
*/
178-
val += offset - __START_KERNEL;
179-
}
180-
r_type = ELF64_R_TYPE(rela->r_info);
181-
rc = arch_kexec_do_relocs(r_type, (void *) loc, val, 0);
182-
if (rc)
183-
error("Unknown relocation type");
184-
}
185-
}
186-
187-
static void kaslr_adjust_got(unsigned long offset) {}
188-
#else
189154
static void kaslr_adjust_relocs(unsigned long min_addr, unsigned long max_addr,
190155
unsigned long offset, unsigned long phys_offset)
191156
{
@@ -212,7 +177,6 @@ static void kaslr_adjust_got(unsigned long offset)
212177
for (entry = (u64 *)vmlinux.got_start; entry < (u64 *)vmlinux.got_end; entry++)
213178
*entry += offset - __START_KERNEL;
214179
}
215-
#endif
216180

217181
/*
218182
* Merge information from several sources into a single ident_map_size value.
@@ -398,14 +362,8 @@ static void kaslr_adjust_vmlinux_info(long offset)
398362
{
399363
vmlinux.bootdata_off += offset;
400364
vmlinux.bootdata_preserved_off += offset;
401-
#ifdef CONFIG_PIE_BUILD
402-
vmlinux.rela_dyn_start += offset;
403-
vmlinux.rela_dyn_end += offset;
404-
vmlinux.dynsym_start += offset;
405-
#else
406365
vmlinux.got_start += offset;
407366
vmlinux.got_end += offset;
408-
#endif
409367
vmlinux.init_mm_off += offset;
410368
vmlinux.swapper_pg_dir_off += offset;
411369
vmlinux.invalid_pg_dir_off += offset;

arch/s390/boot/vmlinux.lds.S

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,12 @@ SECTIONS
9999

100100
_decompressor_end = .;
101101

102-
#ifndef CONFIG_PIE_BUILD
103102
. = ALIGN(4);
104103
.vmlinux.relocs : {
105104
__vmlinux_relocs_64_start = .;
106105
*(.vmlinux.relocs_64)
107106
__vmlinux_relocs_64_end = .;
108107
}
109-
#endif
110108

111109
#ifdef CONFIG_KERNEL_UNCOMPRESSED
112110
. = ALIGN(PAGE_SIZE);

arch/s390/kernel/vmlinux.lds.S

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -192,31 +192,6 @@ SECTIONS
192192

193193
PERCPU_SECTION(0x100)
194194

195-
#ifdef CONFIG_PIE_BUILD
196-
.dynsym ALIGN(8) : {
197-
__dynsym_start = .;
198-
*(.dynsym)
199-
__dynsym_end = .;
200-
}
201-
.rela.dyn ALIGN(8) : {
202-
__rela_dyn_start = .;
203-
*(.rela*)
204-
__rela_dyn_end = .;
205-
}
206-
.dynamic ALIGN(8) : {
207-
*(.dynamic)
208-
}
209-
.dynstr ALIGN(8) : {
210-
*(.dynstr)
211-
}
212-
.hash ALIGN(8) : {
213-
*(.hash)
214-
}
215-
.gnu.hash ALIGN(8) : {
216-
*(.gnu.hash)
217-
}
218-
#endif
219-
220195
. = ALIGN(PAGE_SIZE);
221196
__init_end = .; /* freed after init ends here */
222197

@@ -238,14 +213,8 @@ SECTIONS
238213
QUAD(__boot_data_preserved_start) /* bootdata_preserved_off */
239214
QUAD(__boot_data_preserved_end -
240215
__boot_data_preserved_start) /* bootdata_preserved_size */
241-
#ifdef CONFIG_PIE_BUILD
242-
QUAD(__dynsym_start) /* dynsym_start */
243-
QUAD(__rela_dyn_start) /* rela_dyn_start */
244-
QUAD(__rela_dyn_end) /* rela_dyn_end */
245-
#else
246216
QUAD(__got_start) /* got_start */
247217
QUAD(__got_end) /* got_end */
248-
#endif
249218
QUAD(_eamode31 - _samode31) /* amode31_size */
250219
QUAD(init_mm)
251220
QUAD(swapper_pg_dir)
@@ -281,12 +250,10 @@ SECTIONS
281250
*(.plt) *(.plt.*) *(.iplt) *(.igot .igot.plt)
282251
}
283252
ASSERT(SIZEOF(.plt) == 0, "Unexpected run-time procedure linkages detected!")
284-
#ifndef CONFIG_PIE_BUILD
285253
.rela.dyn : {
286254
*(.rela.*) *(.rela_*)
287255
}
288256
ASSERT(SIZEOF(.rela.dyn) == 0, "Unexpected run-time relocations (.rela) detected!")
289-
#endif
290257

291258
/* Sections to be discarded */
292259
DISCARDS

0 commit comments

Comments
 (0)