Skip to content

Commit 5249edc

Browse files
committed
Simplify
1 parent 1870845 commit 5249edc

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

src/passes.cc

+2-23
Original file line numberDiff line numberDiff line change
@@ -2435,27 +2435,6 @@ static void set_virtual_addresses_regular(Context<E> &ctx) {
24352435
std::vector<Chunk<E> *> &chunks = ctx.chunks;
24362436
u64 addr = ctx.arg.image_base;
24372437

2438-
// TLS chunks alignments are special: in addition to having their virtual
2439-
// addresses aligned, they also have to be aligned when the region of
2440-
// tls_begin is copied to a new thread's storage area. In other words, their
2441-
// offset against tls_begin also has to be aligned.
2442-
//
2443-
// A good way to achieve this is to take the largest alignment requirement
2444-
// of all TLS sections and make tls_begin also aligned to that.
2445-
Chunk<E> *first_tls_chunk = nullptr;
2446-
u64 tls_alignment = 1;
2447-
for (Chunk<E> *chunk : chunks) {
2448-
if (chunk->shdr.sh_flags & SHF_TLS) {
2449-
if (!first_tls_chunk)
2450-
first_tls_chunk = chunk;
2451-
tls_alignment = std::max(tls_alignment, (u64)chunk->shdr.sh_addralign);
2452-
}
2453-
}
2454-
2455-
auto alignment = [&](Chunk<E> *chunk) {
2456-
return chunk == first_tls_chunk ? tls_alignment : (u64)chunk->shdr.sh_addralign;
2457-
};
2458-
24592438
auto is_tbss = [](Chunk<E> *chunk) {
24602439
return (chunk->shdr.sh_type == SHT_NOBITS) && (chunk->shdr.sh_flags & SHF_TLS);
24612440
};
@@ -2527,7 +2506,7 @@ static void set_virtual_addresses_regular(Context<E> &ctx) {
25272506
if (is_tbss(chunks[i])) {
25282507
u64 addr2 = addr;
25292508
for (;;) {
2530-
addr2 = align_to(addr2, alignment(chunks[i]));
2509+
addr2 = align_to(addr2, chunks[i]->shdr.sh_addralign);
25312510
chunks[i]->shdr.sh_addr = addr2;
25322511
addr2 += chunks[i]->shdr.sh_size;
25332512
if (i + 2 == chunks.size() || !is_tbss(chunks[i + 1]))
@@ -2537,7 +2516,7 @@ static void set_virtual_addresses_regular(Context<E> &ctx) {
25372516
continue;
25382517
}
25392518

2540-
addr = align_to(addr, alignment(chunks[i]));
2519+
addr = align_to(addr, chunks[i]->shdr.sh_addralign);
25412520
chunks[i]->shdr.sh_addr = addr;
25422521
addr += chunks[i]->shdr.sh_size;
25432522
}

test/tls-alignment-multi.sh

+16-19
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,39 @@
99
# order to be triggered.
1010

1111
cat <<EOF | $CC -fPIC -c -o $t/a.o -xc -
12-
#include <assert.h>
13-
#include <stdlib.h>
12+
#include <stdio.h>
1413
1514
// .tdata
1615
_Thread_local int x = 42;
16+
1717
// .tbss
18-
__attribute__ ((aligned(64)))
19-
_Thread_local int y = 0;
18+
__attribute__((aligned(64))) _Thread_local int y;
2019
21-
void *verify(void *unused) {
22-
assert((unsigned long)(&y) % 64 == 0);
20+
void *test(void *) {
21+
printf("%p %lu", &y, (unsigned long)&y % 64);
2322
return NULL;
2423
}
2524
EOF
2625

2726
cat <<EOF | $CC -fPIC -c -o $t/b.o -xc -
27+
#include <stdio.h>
2828
#include <pthread.h>
2929
#include <dlfcn.h>
30-
#include <assert.h>
31-
void *(*verify)(void *);
3230
3331
int main() {
34-
void *handle = dlopen("a.so", RTLD_NOW);
35-
assert(handle);
36-
*(void**)(&verify) = dlsym(handle, "verify");
37-
assert(verify);
38-
39-
pthread_t thread;
32+
void *handle = dlopen("c.so", RTLD_NOW);
33+
void *(*test)(void *) = dlsym(handle, "test");
34+
pthread_t th;
4035
41-
verify(NULL);
36+
test(NULL);
37+
printf(" ");
4238
43-
pthread_create(&thread, NULL, verify, NULL);
44-
pthread_join(thread, NULL);
39+
pthread_create(&th, NULL, test, NULL);
40+
pthread_join(th, NULL);
41+
printf("\n");
4542
}
4643
EOF
4744

48-
$CC -B. -shared -o $t/a.so $t/a.o
45+
$CC -B. -shared -o $t/c.so $t/a.o
4946
$CC -B. -ldl -pthread -o $t/exe $t/b.o -Wl,-rpath,$t
50-
$QEMU $t/exe
47+
$QEMU $t/exe | grep -E '^0x[0-9a-f]+ 0 0x[0-9a-f]+ 0$'

0 commit comments

Comments
 (0)