1// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/bss_relocs.h"
6#include "vm/native_symbol.h"
7#include "vm/runtime_entry.h"
8#include "vm/thread.h"
9
10namespace dart {
11
12void BSS::InitializeBSSEntry(BSS::Relocation relocation,
13 uword new_value,
14 uword* bss_start) {
15 std::atomic<uword>* slot = reinterpret_cast<std::atomic<uword>*>(
16 &bss_start[BSS::RelocationIndex(relocation)]);
17 uword old_value = slot->load(std::memory_order_relaxed);
18 // FullSnapshotReader::ReadProgramSnapshot, and thus BSS::Initialize, can
19 // get called multiple times for the same isolate in different threads, though
20 // the initialized value will be consistent and thus change only once. Avoid
21 // calling compare_exchange_strong unless we actually need to change the
22 // value, to avoid spurious read/write races by TSAN.
23 if (old_value == new_value) return;
24 if (!slot->compare_exchange_strong(old_value, new_value,
25 std::memory_order_relaxed)) {
26 RELEASE_ASSERT(old_value == new_value);
27 }
28}
29
30void BSS::Initialize(Thread* current, uword* bss_start, bool vm) {
31 auto const instructions = reinterpret_cast<uword>(
32 current->isolate_group()->source()->snapshot_instructions);
33 uword dso_base;
34 // For non-natively loaded snapshots, this is instead initialized in
35 // LoadedElf::ResolveSymbols().
36 if (NativeSymbolResolver::LookupSharedObject(instructions, &dso_base)) {
37 InitializeBSSEntry(Relocation::InstructionsRelocatedAddress,
38 instructions - dso_base, bss_start);
39 }
40
41 if (!vm) {
42 // Fill values at isolate-only indices.
43 InitializeBSSEntry(Relocation::DRT_GetThreadForNativeCallback,
44 reinterpret_cast<uword>(DLRT_GetThreadForNativeCallback),
45 bss_start);
46 }
47}
48
49} // namespace dart
50