From 76f065ae9161a1770268dda9d1d9caa552ef2c39 Mon Sep 17 00:00:00 2001 From: tkchia Date: Tue, 15 Nov 2022 15:47:05 +0000 Subject: [PATCH] Place TLS sections right after .text, not after embedded zip file --- ape/ape.lds | 57 +++++++++++++++++++++--------------------- libc/calls/metalfile.c | 2 +- libc/stubs/ld.S | 3 +++ 3 files changed, 33 insertions(+), 29 deletions(-) diff --git a/ape/ape.lds b/ape/ape.lds index 9b7f810dc..63a03c8d8 100644 --- a/ape/ape.lds +++ b/ape/ape.lds @@ -349,9 +349,30 @@ SECTIONS { HIDDEN(_etext = .); PROVIDE_HIDDEN(etext = .); /*END: Read Only Data (only needed for initialization) */ -/*END: Read Only Data */ } :Rom +/* initialization image for thread-local storage, this is copied */ +/* out to actual TLS areas at runtime, so just make it read-only */ + .tdata . : { + _tdata_start = .; + *(SORT_BY_ALIGNMENT(.tdata)) + *(SORT_BY_ALIGNMENT(.tdata.*)) + _tdata_end = .; + . = ALIGN(PAGESIZE); + } :Tls :Rom +/*END: Read Only Data */ + +/* this only tells the linker about the layout of uninitialized */ +/* TLS data, and does not advance the linker's location counter */ + .tbss . : { + _tbss_start = .; + *(SORT_BY_ALIGNMENT(.tbss)) + *(SORT_BY_ALIGNMENT(.tbss.*)) + . = ALIGN(TLS_ALIGNMENT); + /* the %fs register is based on this location */ + _tbss_end = .; + } :Tls + .data ALIGN(PAGESIZE) : { /*BEGIN: Read/Write Data */ KEEP(*(SORT_BY_NAME(.piro.data.sort.iat.*))) @@ -385,26 +406,7 @@ SECTIONS { . = ALIGN(PAGESIZE); } :Ram - .tdata . : { - _tdata_start = .; - *(SORT_BY_ALIGNMENT(.tdata)) - *(SORT_BY_ALIGNMENT(.tdata.*)) - _tdata_end = .; - . = ALIGN(PAGESIZE); - } :Tls :Ram - /*END: file content that's loaded by o/s */ -/*BEGIN: bss memory void */ - - .tbss . : { - _tbss_start = .; - *(SORT_BY_ALIGNMENT(.tbss)) - *(SORT_BY_ALIGNMENT(.tbss.*)) - . = ALIGN(TLS_ALIGNMENT); - /* the %fs register is based on this location */ - _tbss_end = .; - } :Tls - /*END: file content */ /*BEGIN: bss memory that's addressable */ @@ -509,7 +511,7 @@ HIDDEN(ape_rom_rva = RVA(ape_rom_vaddr)); HIDDEN(ape_ram_offset = ape_rom_offset + ape_rom_filesz); HIDDEN(ape_ram_vaddr = ADDR(.data)); HIDDEN(ape_ram_paddr = LOADADDR(.data)); -HIDDEN(ape_ram_filesz = SIZEOF(.data) + SIZEOF(.tdata)); +HIDDEN(ape_ram_filesz = SIZEOF(.data)); HIDDEN(ape_ram_memsz = ADDR(.bss) + SIZEOF(.bss) - ape_ram_vaddr); HIDDEN(ape_ram_align = PAGESIZE); HIDDEN(ape_ram_rva = RVA(ape_ram_vaddr)); @@ -533,16 +535,16 @@ HIDDEN(ape_note_align = __SIZEOF_POINTER__); HIDDEN(ape_text_offset = ape_rom_offset + LOADADDR(.text) - ape_rom_paddr); HIDDEN(ape_text_paddr = LOADADDR(.text)); HIDDEN(ape_text_vaddr = ADDR(.text)); -HIDDEN(ape_text_filesz = SIZEOF(.text)); -HIDDEN(ape_text_memsz = SIZEOF(.text)); +HIDDEN(ape_text_filesz = SIZEOF(.text) + SIZEOF(.tdata)); +HIDDEN(ape_text_memsz = SIZEOF(.text) + SIZEOF(.tdata)); HIDDEN(ape_text_align = PAGESIZE); HIDDEN(ape_text_rva = RVA(ape_text_vaddr)); HIDDEN(ape_data_offset = ape_ram_offset + LOADADDR(.data) - ape_ram_paddr); HIDDEN(ape_data_paddr = LOADADDR(.data)); HIDDEN(ape_data_vaddr = ADDR(.data)); -HIDDEN(ape_data_filesz = SIZEOF(.data) + SIZEOF(.tdata)); -HIDDEN(ape_data_memsz = SIZEOF(.data) + SIZEOF(.tdata)); +HIDDEN(ape_data_filesz = SIZEOF(.data)); +HIDDEN(ape_data_memsz = SIZEOF(.data)); HIDDEN(ape_data_align = PAGESIZE); HIDDEN(ape_data_rva = RVA(ape_data_vaddr)); @@ -564,11 +566,10 @@ SHSTUB2(ape_loader_dd_count, #if SupportsMetal() HIDDEN(v_ape_realsectors = - MIN(0x70000 - IMAGE_BASE_REAL, - ROUNDUP(RVA(_tdata_end), 512)) / 512); + MIN(0x70000 - IMAGE_BASE_REAL, ROUNDUP(RVA(_ezip), 512)) / 512); HIDDEN(v_ape_realbytes = v_ape_realsectors * 512); HIDDEN(v_ape_realdwords = v_ape_realsectors * (512 / 4)); -HIDDEN(v_ape_allsectors = ROUNDUP(RVA(_tdata_end), 512) / 512); +HIDDEN(v_ape_allsectors = ROUNDUP(RVA(_ezip), 512) / 512); HIDDEN(v_ape_allbytes = v_ape_allsectors * 512); HIDDEN(v_ape_highsectors = v_ape_allsectors - v_ape_realsectors); TSSDESCSTUB2(_tss, _tss, _tss_end ? _tss_end - _tss - 1 : 0); diff --git a/libc/calls/metalfile.c b/libc/calls/metalfile.c index cc9b5dc56..ddfe07f8a 100644 --- a/libc/calls/metalfile.c +++ b/libc/calls/metalfile.c @@ -61,7 +61,7 @@ textstartup noasan void InitializeMetalFile(void) { * The zipos code will automatically arrange to do this. Alternatively, * user code can STATIC_YOINK this symbol. */ - size_t size = ROUNDUP(_tdata_end - _base, 4096); + size_t size = ROUNDUP(_ezip - _base, 4096); void *copied_base; struct DirectMap dm; dm = sys_mmap_metal(NULL, size, PROT_READ | PROT_WRITE, diff --git a/libc/stubs/ld.S b/libc/stubs/ld.S index b729f18d2..8f314a192 100644 --- a/libc/stubs/ld.S +++ b/libc/stubs/ld.S @@ -29,6 +29,7 @@ _base = 0 ape_xlm = 0 _ehead = 0 + _ezip = 0 _ereal = 0 __privileged_start = 0 __privileged_end = 0 @@ -68,6 +69,7 @@ .globl _edata .globl _ehead .globl _end + .globl _ezip .globl _ereal .globl _etext .globl _tdata_start @@ -97,6 +99,7 @@ .weak _edata .weak _ehead .weak _end + .weak _ezip .weak _ereal .weak _etext .weak _tdata_start