Files
sdk/runtime/vm/elf.h
T
Tess Strickland 84a1b114cd [vm] Add base class for shared object writers.
This CL pulls out the refactorings used to support the new MachOWriter
in a followup CL to allow them to be reviewed separately.

Rename Elf -> ElfWriter. Also rename model classes used by ElfWriter
for concepts that exist both in ELF and Mach-O to ElfX. For example,
the old ELF-specific SymbolTable is renamed to ElfSymbolTable.

Adds SharedObjectWriter to serve as a base class for both ElfWriter
and the upcoming MachOWriter.

Adds a new AbstractWriteStream that serves as a common superclass
of both BaseWriteStream and SharedObjectWriter::WriteStream and
allows the creation of fully delegating WriteStreams that do not
maintain a local buffer.

Abstract the old Elf::SymbolData class into
SharedObjectWriter::SymbolData, which stores an enum value as the type
of the symbol instead of storing the ELF encoding of the type.

Rename the DwarfElfStream (which actually wasn't ELF specific, as
all the ELF-specific DWARF information is handled by ElfWriter) to
DwarfSharedObjectStream and put it in a separate header file.

Rename Image::compiled_to_elf() to Image::compiled_to_shared_object()
and add a separate Image::compiled_to_elf() that checks for the ELF
magic value at the DSO base. Also add Image::shared_object_start()
and Image::build_id_start() to return pointers to the DSO base and
the build ID note, respectively.

Refactor Image::build_id() and Image::build_id_length() to check
compiled_for_elf() prior to decoding the data pointed to by
build_id_start() as an ELF note section.

Create an AOTSnapshotType enum to specific the snapshot writer to use
in CreateAppAOTSnapshot instead of using an as_elf boolean.

TEST=refactorings, so existing tests on ci

Change-Id: Ia3ab37a4dff93b6e00390b123753be5a51fbdaaa
Cq-Include-Trybots: luci.dart.try:vm-aot-linux-debug-x64-try,vm-aot-mac-release-arm64-try,vm-aot-dwarf-linux-product-x64-try,vm-linux-debug-x64-try,vm-mac-debug-arm64-try
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/421301
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Commit-Queue: Tess Strickland <sstrickl@google.com>
2025-05-21 01:36:22 -07:00

120 lines
3.8 KiB
C++

// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
#ifndef RUNTIME_VM_ELF_H_
#define RUNTIME_VM_ELF_H_
#include "platform/globals.h"
#if defined(DART_PRECOMPILER)
#include "vm/allocation.h"
#include "vm/compiler/runtime_api.h"
#include "vm/datastream.h"
#include "vm/growable_array.h"
#include "vm/so_writer.h"
#include "vm/zone.h"
#endif
namespace dart {
// The max page size on all supported architectures. Used to determine
// the alignment of load segments, so that they are guaranteed page-aligned,
// and no ELF section or segment should have a larger alignment.
#if defined(DART_TARGET_OS_LINUX) && defined(TARGET_ARCH_ARM64)
// Some Linux distributions on ARM64 select 64 KB page size.
// Follow LLVM (https://reviews.llvm.org/D25079) and set maximum page size
// to 64 KB for ARM64 Linux builds.
static constexpr intptr_t kElfPageSize = 64 * KB;
#elif defined(DART_TARGET_OS_ANDROID) && defined(TARGET_ARCH_IS_64_BIT)
static constexpr intptr_t kElfPageSize = 64 * KB;
#else
static constexpr intptr_t kElfPageSize = 16 * KB;
#endif
#if defined(DART_PRECOMPILER)
class ProgramTable;
class SectionTable;
class ElfSymbolTable;
class ElfWriter : public SharedObjectWriter {
public:
ElfWriter(Zone* zone,
BaseWriteStream* stream,
Type type,
Dwarf* dwarf = nullptr);
static constexpr intptr_t kPageSize = kElfPageSize;
intptr_t page_size() const override { return kPageSize; }
Output output() const override { return Output::Elf; }
const ElfSymbolTable& symtab() const {
ASSERT(symtab_ != nullptr);
return *symtab_;
}
const SectionTable& section_table() const { return *section_table_; }
void AddText(const char* name,
intptr_t label,
const uint8_t* bytes,
intptr_t size,
const SharedObjectWriter::RelocationArray* relocations,
const SharedObjectWriter::SymbolDataArray* symbol) override;
void AddROData(const char* name,
intptr_t label,
const uint8_t* bytes,
intptr_t size,
const SharedObjectWriter::RelocationArray* relocations,
const SharedObjectWriter::SymbolDataArray* symbols) override;
void Finalize() override;
void AssertConsistency(const SharedObjectWriter* debug) const override {
if (auto* const debug_elf = debug->AsElfWriter()) {
AssertConsistency(this, debug_elf);
} else {
FATAL("Expected both snapshot and debug to be ELF");
}
}
const ElfWriter* AsElfWriter() const override { return this; }
private:
static void AssertConsistency(const ElfWriter* snapshot,
const ElfWriter* debug_info);
static constexpr const char kBuildIdNoteName[] = ".note.gnu.build-id";
static constexpr const char kTextName[] = ".text";
static constexpr const char kDataName[] = ".rodata";
static constexpr const char kBssName[] = ".bss";
static constexpr const char kDynamicTableName[] = ".dynamic";
void CreateBSS();
void GenerateBuildId();
void InitializeSymbolTables();
void FinalizeDwarfSections();
void FinalizeEhFrame();
void ComputeOffsets();
// Contains all sections that will have entries in the section header table.
SectionTable* const section_table_;
// Contains all segments in the program header table. Set after finalizing
// the section table.
ProgramTable* program_table_ = nullptr;
// The static tables are always created for use in relocation calculations,
// even though they may not end up in the final ELF file.
ElfSymbolTable* symtab_ = nullptr;
friend class SectionTable; // For section name static fields.
};
#endif // DART_PRECOMPILER
} // namespace dart
#endif // RUNTIME_VM_ELF_H_