Skip to content

Commit

Permalink
tls: parameterize static tls reservation
Browse files Browse the repository at this point in the history
This patch makes it possible to pass size of the app static TLS
reservation as build parameter to makefile.

It also adds logic calculate what value of this parameter
should be if the resevation is too small for given app.

Signed-off-by: Waldemar Kozaczuk <[email protected]>
  • Loading branch information
wkozaczuk committed Nov 7, 2019
1 parent b0478df commit 2d5f2d5
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ kernel_base := 0x200000
lzkernel_base := 0x100000
kernel_vm_base := 0x40200000

# the default of 64 bytes can be overridden by passing the app_local_exec_tls_size
# environment variable to the make or scripts/build
app_local_exec_tls_size := 0x40

$(out)/arch/x64/boot16.o: $(out)/lzloader.elf
$(out)/boot.bin: arch/x64/boot16.ld $(out)/arch/x64/boot16.o
$(call quiet, $(LD) -o $@ -T $^, LD $@)
Expand Down Expand Up @@ -1886,7 +1890,7 @@ stage1: $(stage1_targets) links

$(out)/loader.elf: $(stage1_targets) arch/$(arch)/loader.ld $(out)/bootfs.o
$(call quiet, $(LD) -o $@ --defsym=OSV_KERNEL_BASE=$(kernel_base) \
--defsym=OSV_KERNEL_VM_BASE=$(kernel_vm_base) --defsym=OSV_KERNEL_VM_SHIFT=$(kernel_vm_shift) \
--defsym=OSV_KERNEL_VM_BASE=$(kernel_vm_base) --defsym=OSV_KERNEL_VM_SHIFT=$(kernel_vm_shift) --defsym=APP_LOCAL_EXEC_TLS_SIZE=$(app_local_exec_tls_size) \
-Bdynamic --export-dynamic --eh-frame-hdr --enable-new-dtags \
$(^:%.ld=-T %.ld) \
--whole-archive \
Expand Down
2 changes: 1 addition & 1 deletion arch/aarch64/loader.ld
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ SECTIONS
*(.tbss .tbss.* .gnu.linkonce.tb.*) *(.tcommon)
_pie_static_tls_start = .;
/* This is a reserve intended for executables' (pie or non-pie) TLS block */
. = . + 64;
. = . + APP_LOCAL_EXEC_TLS_SIZE;
_pie_static_tls_end = .;
} : tls : text
.tls_template_size = SIZEOF(.tdata) + SIZEOF(.tbss);
Expand Down
2 changes: 1 addition & 1 deletion arch/x64/loader.ld
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ SECTIONS
*(.tbss .tbss.* .gnu.linkonce.tb.*)
_pie_static_tls_start = .;
/* This is a reserve intended for executables' (pie or non-pie) TLS block */
. = . + 64;
. = . + APP_LOCAL_EXEC_TLS_SIZE;
. = ALIGN(64);
_pie_static_tls_end = .;
} :tls :text
Expand Down
19 changes: 13 additions & 6 deletions core/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,20 @@ void object::load_segments()
abort("Statically linked executables are not supported!\n");
}
if (_is_executable && _tls_segment) {
auto needed_tls_size = get_aligned_tls_size();
auto app_tls_size = get_aligned_tls_size();
ulong pie_static_tls_maximum_size = &_pie_static_tls_end - &_pie_static_tls_start;
if (needed_tls_size > pie_static_tls_maximum_size) {
std::cout << "WARNING: " << pathname() << " is a PIE using TLS of size " << needed_tls_size
<< " which is greater than " << pie_static_tls_maximum_size << " bytes limit. "
<< "Either increase the size of TLS reserve in arch/x64/loader.ld or "
<< "link with '-shared' instead of '-pie'.\n";
if (app_tls_size > pie_static_tls_maximum_size) {
// The reservation at the end of the kernel TLS is NOT big enough
// to hold the app static TLS. Let us calculate what the reservation should be
// and inform user how to relink kernel
auto kernel_tls_size = sched::kernel_tls_size();
auto kernel_tls_used_size = kernel_tls_size - pie_static_tls_maximum_size;
auto kernel_tls_needed_size = align_up(kernel_tls_used_size + app_tls_size, 64UL);
auto app_tls_needed_size = kernel_tls_needed_size - kernel_tls_used_size;
std::cout << "WARNING: " << pathname() << " is a PIE using TLS of size " << app_tls_size
<< " which is greater than the " << pie_static_tls_maximum_size << " bytes limit. "
<< "Either re-link the kernel by adding 'app_local_exec_tls_size=" << app_tls_needed_size
<< "' to ./scripts/build or re-link the app with '-shared' instead of '-pie'.\n";
}
}
}
Expand Down
1 change: 1 addition & 0 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ usage() {
fs=zfs|rofs|ramfs Specify the filesystem of the image partition
fs_size=N Specify the size of the image in bytes
fs_size_mb=N Specify the size of the image in MiB
app_local_exec_tls_size=N Specify the size of app local TLS in bytes; the default is 64
usrskel=<*.skel> Specify the base manifest for the image
<module_makefile_arg>=<value> Pass value of module_makefile_arg to an app/module makefile
(can be used to customize specific app/module build process)
Expand Down

0 comments on commit 2d5f2d5

Please sign in to comment.