1// Copyright 2019 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/snapshot/embedded/platform-embedded-file-writer-mac.h"
6
7#include "src/objects/code.h"
8
9namespace v8 {
10namespace internal {
11
12namespace {
13
14const char* DirectiveAsString(DataDirective directive) {
15  switch (directive) {
16    case kByte:
17      return ".byte";
18    case kLong:
19      return ".long";
20    case kQuad:
21      return ".quad";
22    case kOcta:
23      return ".octa";
24  }
25  UNREACHABLE();
26}
27
28}  // namespace
29
30void PlatformEmbeddedFileWriterMac::SectionText() { fprintf(fp_, ".text\n"); }
31
32void PlatformEmbeddedFileWriterMac::SectionData() { fprintf(fp_, ".data\n"); }
33
34void PlatformEmbeddedFileWriterMac::SectionRoData() {
35  fprintf(fp_, ".const_data\n");
36}
37
38void PlatformEmbeddedFileWriterMac::DeclareUint32(const char* name,
39                                                  uint32_t value) {
40  DeclareSymbolGlobal(name);
41  DeclareLabel(name);
42  IndentedDataDirective(kLong);
43  fprintf(fp_, "%d", value);
44  Newline();
45}
46
47void PlatformEmbeddedFileWriterMac::DeclarePointerToSymbol(const char* name,
48                                                           const char* target) {
49  DeclareSymbolGlobal(name);
50  DeclareLabel(name);
51  fprintf(fp_, "  %s _%s\n", DirectiveAsString(PointerSizeDirective()), target);
52}
53
54void PlatformEmbeddedFileWriterMac::DeclareSymbolGlobal(const char* name) {
55  // TODO(jgruber): Investigate switching to .globl. Using .private_extern
56  // prevents something along the compilation chain from messing with the
57  // embedded blob. Using .global here causes embedded blob hash verification
58  // failures at runtime.
59  fprintf(fp_, ".private_extern _%s\n", name);
60}
61
62void PlatformEmbeddedFileWriterMac::AlignToCodeAlignment() {
63#if V8_TARGET_ARCH_X64
64  // On x64 use 64-bytes code alignment to allow 64-bytes loop header alignment.
65  STATIC_ASSERT(64 >= kCodeAlignment);
66  fprintf(fp_, ".balign 64\n");
67#elif V8_TARGET_ARCH_PPC64
68  // 64 byte alignment is needed on ppc64 to make sure p10 prefixed instructions
69  // don't cross 64-byte boundaries.
70  STATIC_ASSERT(64 >= kCodeAlignment);
71  fprintf(fp_, ".balign 64\n");
72#elif V8_TARGET_ARCH_ARM64
73  // ARM64 macOS has a 16kiB page size. Since we want to remap it on the heap,
74  // needs to be page-aligned.
75  fprintf(fp_, ".balign 16384\n");
76#else
77  STATIC_ASSERT(32 >= kCodeAlignment);
78  fprintf(fp_, ".balign 32\n");
79#endif
80}
81
82void PlatformEmbeddedFileWriterMac::PaddingAfterCode() {
83#if V8_TARGET_ARCH_ARM64
84  // ARM64 macOS has a 16kiB page size. Since we want to remap builtins on the
85  // heap, make sure that the trailing part of the page doesn't contain anything
86  // dangerous.
87  fprintf(fp_, ".balign 16384\n");
88#endif
89}
90
91void PlatformEmbeddedFileWriterMac::AlignToDataAlignment() {
92  STATIC_ASSERT(8 >= Code::kMetadataAlignment);
93  fprintf(fp_, ".balign 8\n");
94}
95
96void PlatformEmbeddedFileWriterMac::Comment(const char* string) {
97  fprintf(fp_, "// %s\n", string);
98}
99
100void PlatformEmbeddedFileWriterMac::DeclareLabel(const char* name) {
101  fprintf(fp_, "_%s:\n", name);
102}
103
104void PlatformEmbeddedFileWriterMac::SourceInfo(int fileid, const char* filename,
105                                               int line) {
106  fprintf(fp_, ".loc %d %d\n", fileid, line);
107}
108
109// TODO(mmarchini): investigate emitting size annotations for OS X
110void PlatformEmbeddedFileWriterMac::DeclareFunctionBegin(const char* name,
111                                                         uint32_t size) {
112  if (ENABLE_CONTROL_FLOW_INTEGRITY_BOOL) {
113    DeclareSymbolGlobal(name);
114  }
115
116  DeclareLabel(name);
117
118  // TODO(mvstanton): Investigate the proper incantations to mark the label as
119  // a function on OSX.
120}
121
122void PlatformEmbeddedFileWriterMac::DeclareFunctionEnd(const char* name) {}
123
124void PlatformEmbeddedFileWriterMac::FilePrologue() {}
125
126void PlatformEmbeddedFileWriterMac::DeclareExternalFilename(
127    int fileid, const char* filename) {
128  fprintf(fp_, ".file %d \"%s\"\n", fileid, filename);
129}
130
131void PlatformEmbeddedFileWriterMac::FileEpilogue() {}
132
133int PlatformEmbeddedFileWriterMac::IndentedDataDirective(
134    DataDirective directive) {
135  return fprintf(fp_, "  %s ", DirectiveAsString(directive));
136}
137
138}  // namespace internal
139}  // namespace v8
140