Skip to content

Commit c9991ab

Browse files
Merge pull request #61279 from apple/rdar100130950
[IRGen] Emit bit for @main into entry section.
2 parents ea3947a + 71d9938 commit c9991ab

File tree

8 files changed

+128
-6
lines changed

8 files changed

+128
-6
lines changed

include/swift/AST/Module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,8 @@ class ModuleDecl
841841
return EntryPointInfo.hasEntryPoint();
842842
}
843843

844+
NominalTypeDecl *getMainTypeDecl() const;
845+
844846
/// Returns the associated clang module if one exists.
845847
const clang::Module *findUnderlyingClangModule() const;
846848

lib/AST/Module.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,22 @@ bool SourceFile::registerMainDecl(ValueDecl *mainDecl, SourceLoc diagLoc) {
18761876
return false;
18771877
}
18781878

1879+
NominalTypeDecl *ModuleDecl::getMainTypeDecl() const {
1880+
if (!EntryPointInfo.hasEntryPoint())
1881+
return nullptr;
1882+
auto *file = EntryPointInfo.getEntryPointFile();
1883+
if (!file)
1884+
return nullptr;
1885+
auto *mainDecl = file->getMainDecl();
1886+
if (!mainDecl)
1887+
return nullptr;
1888+
auto *func = dyn_cast<FuncDecl>(file->getMainDecl());
1889+
if (!func)
1890+
return nullptr;
1891+
auto *nominalType = dyn_cast<NominalTypeDecl>(func->getDeclContext());
1892+
return nominalType;
1893+
}
1894+
18791895
bool ModuleDecl::registerEntryPointFile(FileUnit *file, SourceLoc diagLoc,
18801896
Optional<ArtificialMainKind> kind) {
18811897
if (!EntryPointInfo.hasEntryPoint()) {

lib/IRGen/GenDecl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2092,6 +2092,14 @@ void IRGenerator::emitEntryPointInfo() {
20922092
auto entrypointInfo = builder.beginStruct();
20932093
entrypointInfo.addCompactFunctionReference(
20942094
IGM.getAddrOfSILFunction(entrypoint, NotForDefinition));
2095+
uint32_t flags = 0;
2096+
enum EntryPointFlags : unsigned {
2097+
HasAtMainTypeFlag = 1 << 0,
2098+
};
2099+
if (auto *mainTypeDecl = SIL.getSwiftModule()->getMainTypeDecl()) {
2100+
flags |= HasAtMainTypeFlag;
2101+
}
2102+
entrypointInfo.addInt(IGM.Int32Ty, flags);
20952103
auto var = entrypointInfo.finishAndCreateGlobal(
20962104
"\x01l_entry_point", Alignment(4),
20972105
/*isConstant*/ true, llvm::GlobalValue::PrivateLinkage);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print("hello world")
2+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-clang %s -std=c++11 -isysroot %sdk -o %t/main
3+
// RUN: %target-codesign %t/main
4+
// RUN: %target-swift-frontend -c %S/Inputs/entry-point-section/main.swift -O -o %t/howdy.o -module-name Howdy
5+
// RUN: %target-ld %t/howdy.o -syslibroot %sdk -lSystem -dylib -o %t/libHowdy.dylib
6+
// RUN: %target-codesign %t/libHowdy.dylib
7+
// RUN: %target-run %t/main %t/libHowdy.dylib | %FileCheck %s
8+
9+
// REQUIRES: VENDOR=apple
10+
// REQUIRES: executable_test
11+
// UNSUPPORTED: remote_run
12+
13+
#include <dlfcn.h>
14+
#include <mach-o/dyld.h>
15+
#include <mach-o/getsect.h>
16+
#include <ptrauth.h>
17+
#include <stdio.h>
18+
#include <string.h>
19+
20+
#if __POINTER_WIDTH__ == 64
21+
using mach_header_platform = mach_header_64;
22+
#else
23+
using mach_header_platform = mach_header;
24+
#endif
25+
26+
int main(int argc, char *argv[]) {
27+
if (argc != 2) {
28+
printf("no argument!\n");
29+
return 1;
30+
}
31+
auto *dylibPath = argv[1];
32+
auto *handle = dlopen(dylibPath, RTLD_LAZY);
33+
if (!handle) {
34+
printf("no library!\n");
35+
return 1;
36+
}
37+
38+
using MainFunction = void(int, char *[]);
39+
MainFunction *mainFunction = nullptr;
40+
for (int index = 0; index < _dyld_image_count(); ++index) {
41+
auto *imageName = _dyld_get_image_name(index);
42+
if (strcmp(dylibPath, imageName)) {
43+
printf("skipping %s\n", imageName);
44+
continue;
45+
}
46+
auto *header = reinterpret_cast<const mach_header_platform *>(
47+
_dyld_get_image_header(index));
48+
size_t size;
49+
auto *data = getsectiondata(header, "__TEXT", "__swift5_entry", &size);
50+
int32_t offset = *reinterpret_cast<int32_t *>(data);
51+
if (size >= 8) {
52+
auto flags = *(reinterpret_cast<int32_t *>(data) + 1);
53+
enum EntryPointFlags : unsigned {
54+
HasAtMainTypeFlag = 1 << 0,
55+
};
56+
printf("flags: %d\n", flags);
57+
bool isAtMainEntryPoint = flags & HasAtMainTypeFlag;
58+
if (!isAtMainEntryPoint) {
59+
printf("no @main entry point!\n"); // CHECK: no @main entry point!
60+
continue;
61+
}
62+
}
63+
mainFunction = reinterpret_cast<MainFunction *>(
64+
ptrauth_sign_unauthenticated(
65+
reinterpret_cast<void *>(
66+
reinterpret_cast<long>(data) + offset
67+
),
68+
ptrauth_key_function_pointer,
69+
ptrauth_function_pointer_type_discriminator(MainFunction)
70+
)
71+
);
72+
73+
74+
break;
75+
}
76+
if (!mainFunction) {
77+
printf("no function!\n"); // CHECK: no function!
78+
return 0;
79+
}
80+
mainFunction(argc, argv);
81+
return 1;
82+
}

test/IRGen/entrypoint-section-run.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ int main(int argc, char *argv[]) {
4747
size_t size;
4848
auto *data = getsectiondata(header, "__TEXT", "__swift5_entry", &size);
4949
int32_t offset = *reinterpret_cast<int32_t *>(data);
50+
if (size >= 8) {
51+
auto flags = *(reinterpret_cast<int32_t *>(data) + 1);
52+
enum EntryPointFlags : unsigned {
53+
HasAtMainTypeFlag = 1 << 0,
54+
};
55+
printf("flags: %d\n", flags);
56+
bool isAtMainEntryPoint = flags & HasAtMainTypeFlag;
57+
if (!isAtMainEntryPoint) {
58+
printf("no @main entry point!\n");
59+
continue;
60+
}
61+
}
5062
mainFunction = reinterpret_cast<MainFunction *>(
5163
ptrauth_sign_unauthenticated(
5264
reinterpret_cast<void *>(
@@ -60,7 +72,7 @@ int main(int argc, char *argv[]) {
6072
break;
6173
}
6274
if (!mainFunction) {
63-
printf("no function!");
75+
printf("no function!\n");
6476
return 1;
6577
}
6678
mainFunction(argc, argv); // CHECK: howdy mundo

test/IRGen/unused.sil

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ bb0(%0 : $Int32, %1 : $UnsafeMutablePointer<Optional<UnsafeMutablePointer<Int8>>
5252
return %3 : $Int32 // id: %4
5353
}
5454

55-
// CHECK-macho: @"\01l_entry_point" = private constant { i32 } { i32 trunc (i64 sub (i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64 ptrtoint ({ i32 }* @"\01l_entry_point" to i64)) to i32) }, section "__TEXT, __swift5_entry, regular, no_dead_strip", align 4
56-
// CHECK-elf: @"\01l_entry_point" = private constant { i32 } { i32 trunc (i64 sub (i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64 ptrtoint ({ i32 }* @"\01l_entry_point" to i64)) to i32) }, section "swift5_entry", align 4
55+
// CHECK-macho: @"\01l_entry_point" = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64 ptrtoint ({ i32, i32 }* @"\01l_entry_point" to i64)) to i32), i32 0 }, section "__TEXT, __swift5_entry, regular, no_dead_strip", align 4
56+
// CHECK-elf: @"\01l_entry_point" = private constant { i32, i32 } { i32 trunc (i64 sub (i64 ptrtoint (i32 (i32, i8**)* @main to i64), i64 ptrtoint ({ i32, i32 }* @"\01l_entry_point" to i64)) to i32), i32 0 }, section "swift5_entry", align 4
5757

58-
// CHECK-macho: @llvm.used = appending global [4 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast ({ i32 }* @"\01l_entry_point" to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata"
59-
// CHECK-elf: @llvm.compiler.used = appending global [5 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast ({ i32 }* @"\01l_entry_point" to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*), i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* @_swift1_autolink_entries, i32 0, i32 0)], section "llvm.metadata"
58+
// CHECK-macho: @llvm.used = appending global [4 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast ({ i32, i32 }* @"\01l_entry_point" to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*)], section "llvm.metadata"
59+
// CHECK-elf: @llvm.compiler.used = appending global [5 x i8*] [i8* bitcast (void ()* @frieda to i8*), i8* bitcast (i32 (i32, i8**)* @main to i8*), i8* bitcast ({ i32, i32 }* @"\01l_entry_point" to i8*), i8* bitcast (i16* @__swift_reflection_version to i8*), i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* @_swift1_autolink_entries, i32 0, i32 0)], section "llvm.metadata"
6060

6161
// CHECK: define linkonce_odr hidden swiftcc void @qux()
6262
// CHECK: define hidden swiftcc void @fred()

test/lit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ config.test_format = swift_test.SwiftTest(coverage_mode=config.coverage_mode,
193193

194194
# suffixes: A list of file extensions to treat as test files.
195195
config.suffixes = ['.swift', '.ll', '.sil', '.gyb', '.m', '.c',
196-
'.swiftinterface', '.test-sh', '.test']
196+
'.swiftinterface', '.test-sh', '.test', '.cpp']
197197

198198
# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
199199
# subdirectories contain auxiliary inputs for various tests in their parent

0 commit comments

Comments
 (0)