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
9 namespace v8 {
10 namespace internal {
11
12 namespace {
13
DirectiveAsString(DataDirective directive)14 const 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
SectionText()30 void PlatformEmbeddedFileWriterMac::SectionText() { fprintf(fp_, ".text\n"); }
31
SectionData()32 void PlatformEmbeddedFileWriterMac::SectionData() { fprintf(fp_, ".data\n"); }
33
SectionRoData()34 void PlatformEmbeddedFileWriterMac::SectionRoData() {
35 fprintf(fp_, ".const_data\n");
36 }
37
DeclareUint32(const char * name,uint32_t value)38 void 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
DeclarePointerToSymbol(const char * name,const char * target)47 void 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
DeclareSymbolGlobal(const char * name)54 void 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
AlignToCodeAlignment()62 void 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
PaddingAfterCode()82 void 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
AlignToDataAlignment()91 void PlatformEmbeddedFileWriterMac::AlignToDataAlignment() {
92 STATIC_ASSERT(8 >= Code::kMetadataAlignment);
93 fprintf(fp_, ".balign 8\n");
94 }
95
Comment(const char * string)96 void PlatformEmbeddedFileWriterMac::Comment(const char* string) {
97 fprintf(fp_, "// %s\n", string);
98 }
99
DeclareLabel(const char * name)100 void PlatformEmbeddedFileWriterMac::DeclareLabel(const char* name) {
101 fprintf(fp_, "_%s:\n", name);
102 }
103
SourceInfo(int fileid,const char * filename,int line)104 void 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
DeclareFunctionBegin(const char * name,uint32_t size)110 void 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
DeclareFunctionEnd(const char * name)122 void PlatformEmbeddedFileWriterMac::DeclareFunctionEnd(const char* name) {}
123
FilePrologue()124 void PlatformEmbeddedFileWriterMac::FilePrologue() {}
125
DeclareExternalFilename(int fileid,const char * filename)126 void PlatformEmbeddedFileWriterMac::DeclareExternalFilename(
127 int fileid, const char* filename) {
128 fprintf(fp_, ".file %d \"%s\"\n", fileid, filename);
129 }
130
FileEpilogue()131 void PlatformEmbeddedFileWriterMac::FileEpilogue() {}
132
IndentedDataDirective(DataDirective directive)133 int PlatformEmbeddedFileWriterMac::IndentedDataDirective(
134 DataDirective directive) {
135 return fprintf(fp_, " %s ", DirectiveAsString(directive));
136 }
137
138 } // namespace internal
139 } // namespace v8
140