Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for ELF thread local storage segments #96

Merged
merged 1 commit into from
Feb 25, 2020

Conversation

phil-opp
Copy link
Member

@phil-opp phil-opp commented Feb 19, 2020

This reports the thread local storage segment of the kernel (if present) in the boot information. This should make it possible to use the unstable #[thread_local] attribute. Since the TLS template overlaps with parts of a LOAD segment, no additional mapping needs to be done.

Note that the TLS template should not be used directly. Instead, the kernel should create a copy the TLS template for each thread and then set up the fs register accordingly. See https://uclibc.org/docs/tls.pdf for more information.

This code only works for a single TLS segment. If more than one TLS segment are present in the executable, the bootloader errors. We can of course lift this restriction if this becomes a problem in practice.

Fixes #95

@phil-opp phil-opp requested a review from a team February 19, 2020 15:49
@phil-opp
Copy link
Member Author

@vinaychandra Could you try whether this change works for you?

@phil-opp
Copy link
Member Author

Ah, it seems like I understood something wrong. I will create a new version.

@phil-opp
Copy link
Member Author

With the new version, I can now successfully read the TLS template info for a small example:

#[thread_local]
pub static mut DATA: u64 = 0;
#[thread_local]
pub static BSS: u64 = 0;

fn kernel_main(boot_info: &'static BootInfo) -> ! {
    println!("Hello World{}", "!");
    println!("TLS INFO: {:x?}", boot_info.tls_template());

    // this panics because we did not set up the fs register yet
    println!("DATA: {}, BSS: {}", unsafe { DATA }, BSS);
}

The result is the following:

image

I get the same values with readelf -l target/x86_64-blog_os/debug/blog_os:

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000000200040 0x0000000000200040
                 0x00000000000001c0 0x00000000000001c0  R      0x8
  LOAD           0x0000000000000000 0x0000000000200000 0x0000000000200000
                 0x0000000000003544 0x0000000000003544  R      0x1000
  LOAD           0x0000000000004000 0x0000000000204000 0x0000000000204000
                 0x000000000001f23c 0x000000000001f23c  R E    0x1000
  LOAD           0x0000000000024000 0x0000000000224000 0x0000000000224000
                 0x00000000000012d0 0x00000000000012d0  RW     0x1000
  LOAD           0x0000000000026000 0x0000000000226000 0x0000000000226000
                 0x000000000000000e 0x0000000000002178  RW     0x1000
  TLS            0x0000000000024000 0x0000000000224000 0x0000000000224000
                 0x0000000000000008 0x0000000000000010  R      0x8
  GNU_RELRO      0x0000000000024000 0x0000000000224000 0x0000000000224000
                 0x00000000000012d0 0x0000000000002000  R      0x1
  GNU_STACK      0x0000000000000000 0x0000000000000000 0x0000000000000000
                 0x0000000000000000 0x0000000000000000  RW     0x0

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .rodata .eh_frame 
   02     .text 
   03     .tdata .data.rel.ro .got 
   04     .data .bss 
   05     .tdata .tbss 
   06     .tdata .data.rel.ro .got 
   07     

@phil-opp phil-opp merged commit 6d2bcb3 into master Feb 25, 2020
@bors bors bot deleted the elf-tls-sections branch February 25, 2020 11:54
@phil-opp
Copy link
Member Author

phil-opp commented Feb 25, 2020

Published as version 0.8.8

phil-opp added a commit that referenced this pull request Feb 25, 2020
vinaychandra pushed a commit to vinaychandra/bootloader that referenced this pull request May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable tls sections
2 participants