]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/ukreloc: Add ld script for sections required for a relocatable image
authorSergiu Moga <sergiu.moga@protonmail.com>
Sun, 19 Mar 2023 17:17:08 +0000 (19:17 +0200)
committerUnikraft <monkey@unikraft.io>
Fri, 11 Aug 2023 08:11:27 +0000 (08:11 +0000)
Add a separate Linker Script containing all the required sections
to achieve positional independence and the ability to self relocate.

The `.uk_reloc`, `.dynsym` and `.rela.dyn` sections will be part of the
main data segment and the last two will be stripped in the end, without
causing a hole in memory, due to the fact that `.uk_reloc` already
contains the forced `struct uk_reloc` relocations and, after being
updated, will also contain the `struct uk_reloc` equivalents of
`.rela.dyn`'s entries, which will be of the same size, so the `.bss`
section will stay in place.

The `.dynamic` and `.dynstr` sections become irrelevant in the final image
so we will strip them as well. In order not to cause a memory hole, place
them after `.comment` section.

Signed-off-by: Sergiu Moga <sergiu.moga@protonmail.com>
Reviewed-by: Dragos Petre <dragos.petre27@gmail.com>
Reviewed-by: Michalis Pappas <michalis@unikraft.io>
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io>
Approved-by: Razvan Deaconescu <razvand@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #772

lib/ukreloc/Makefile.uk
lib/ukreloc/reloc.lds.S [new file with mode: 0644]
plat/common/include/uk/plat/common/sections.h

index cefeabdbc88367abdd8c1f7798a67f2f0f17d771..af44e27e787c7525d31eff2ba31ac24d89bcfbde 100644 (file)
@@ -8,3 +8,9 @@ CXXINCLUDES-y += -I$(LIBUKRELOC_BASE)/include
 LIBUKRELOC_ASINCLUDES-y  += -I$(UK_PLAT_COMMON_BASE)/include
 LIBUKRELOC_CINCLUDES-y   += -I$(UK_PLAT_COMMON_BASE)/include
 LIBUKRELOC_CXXINCLUDES-y += -I$(UK_PLAT_COMMON_BASE)/include
+
+LIBUKRELOC_ASFLAGS-y  += -DUK_USE_SECTION_SEGMENTS
+LIBUKRELOC_CFLAGS-y   += -DUK_USE_SECTION_SEGMENTS
+LIBUKRELOC_CXXFLAGS-y += -DUK_USE_SECTION_SEGMENTS
+
+LIBUKRELOC_SRCS-$(CONFIG_LIBUKRELOC) += $(LIBUKRELOC_BASE)/reloc.lds.S
diff --git a/lib/ukreloc/reloc.lds.S b/lib/ukreloc/reloc.lds.S
new file mode 100644 (file)
index 0000000..bda4d90
--- /dev/null
@@ -0,0 +1,66 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the BSD-3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ */
+
+#include <uk/arch/limits.h> /* for __PAGE_SIZE */
+#include <uk/plat/common/common.lds.h> /* for UK_SEGMENT_DATA */
+
+#define UKRELOC_SIGNATURE_SIZE         4
+
+PHDRS
+{
+       dynamic PT_DYNAMIC;
+}
+
+SECTIONS
+{
+       . = ALIGN(__PAGE_SIZE);
+       .uk_reloc :
+       {
+               _uk_reloc_start = .;
+               /* Start gathering static uk_reloc from the signature onwards */
+               . += UKRELOC_SIGNATURE_SIZE;
+               KEEP(*(.uk_reloc))
+       } UK_SEGMENT_DATA
+
+       /* For whatever reason, .dynsym wants to stay before .rela.dyn.
+        * This does not bother us, even if we strip it, since we are compiling
+        * statically, .dynsym will be insignificantly small.
+        */
+       .dynsym :
+       {
+               *(.dynsym)
+       } UK_SEGMENT_DATA
+
+       .rela.dyn :
+       {
+               *(.rela)
+               *(.rela.*)
+       } UK_SEGMENT_DATA
+       _uk_reloc_end = .;
+}
+INSERT BEFORE .bss
+
+/******************************************************************************/
+/****************************** .bss section **********************************/
+/******************************************************************************/
+
+/******************************************************************************/
+/**************************** .comment section ********************************/
+/******************************************************************************/
+
+SECTIONS
+{
+       .dynamic :
+       {
+               *(.dynamic)
+       } :dynamic UK_SEGMENT_DATA
+
+       .dynstr :
+       {
+               *(.dynstr)
+       } UK_SEGMENT_DATA
+}
+INSERT AFTER .comment
index a2122b126da456cf5450f986660021b8e30fe28f..0e24dc9ebbce33d3ee1f2d854c700bffa485df00 100644 (file)
@@ -71,6 +71,9 @@ extern char _tls_start[], _tls_end[];
 /* _etdata: denotes end of .tdata (and start of .tbss */
 extern char _etdata[];
 
+/* [_uk_reloc_start, _uk_reloc_end]: contains *(.ukeloc) */
+extern char _uk_reloc_start[], _uk_reloc_end[];
+
 /* __bss_start: start of BSS sections */
 extern char __bss_start[];
 
@@ -93,6 +96,8 @@ extern char _end[];
 #define __EDATA                 __uk_image_symbol(_edata)
 #define __CTORS                 __uk_image_symbol(_ctors)
 #define __ECTORS                __uk_image_symbol(_ectors)
+#define __UKRELOC_START         __uk_image_symbol(_uk_reloc_start)
+#define __UKRELOC_END           __uk_image_symbol(_uk_reloc_end)
 #define __BSS_START             __uk_image_symbol(__bss_start)
 #define __END                   __uk_image_symbol(_end)