Skip to content

Commit

Permalink
Fix non-deterministic behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
rui314 committed Apr 18, 2024
1 parent 7434465 commit 6463a7c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
20 changes: 11 additions & 9 deletions elf/passes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -623,18 +623,18 @@ void create_output_sections(Context<E> &ctx) {
continue;
}

OutputSectionKey key = get_output_section_key(ctx, *isec);
auto get_or_insert = [&] {
OutputSectionKey key = get_output_section_key(ctx, *isec);

if (auto it = cache.find(key); it != cache.end()) {
isec->output_section = it->second;
continue;
}
if (auto it = cache.find(key); it != cache.end())
return it->second;

auto get_or_insert = [&] {
{
std::shared_lock lock(mu);
if (auto it = map.find(key); it != map.end())
if (auto it = map.find(key); it != map.end()) {
cache.insert({key, it->second});
return it->second;
}
}

std::unique_ptr<OutputSection<E>> osec =
Expand All @@ -646,13 +646,15 @@ void create_output_sections(Context<E> &ctx) {

if (inserted)
ctx.osec_pool.emplace_back(std::move(osec));
cache.insert({key, it->second});
return ret;
};

OutputSection<E> *osec = get_or_insert();
osec->sh_flags |= sh_flags & ~SHF_GROUP;
sh_flags &= ~SHF_GROUP;
if ((osec->sh_flags & sh_flags) != sh_flags)
osec->sh_flags |= sh_flags;
isec->output_section = osec;
cache.insert({key, osec});
}
});

Expand Down
24 changes: 24 additions & 0 deletions test/elf/section-attributes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
. $(dirname $0)/common.inc

cat <<EOF | $CC -o $t/a.o -c -xassembler -
.section .foobar,"aw"
.ascii "foo\0"
EOF

cat <<EOF | $CC -o $t/b.o -c -xassembler -
.section .foobar,"a"
.ascii "bar\0"
EOF

cat <<EOF | $CC -o $t/c.o -c -xassembler -
.section .foobar,"ax"
.ascii "bar\0"
EOF

cat <<EOF | $CC -o $t/d.o -c -xc -
int main() {}
EOF

$CC -B. -o $t/exe $t/a.o $t/b.o $t/c.o $t/d.o
readelf -W --sections $t/exe | grep -q 'foobar.*WAX'

0 comments on commit 6463a7c

Please sign in to comment.