|
| 1 | +/* |
| 2 | +EXTRA_SOURCES: extra-files/sectiondefs.d |
| 3 | +EXTRA_FILES: extra-files/sectiondefs.d |
| 4 | +REQUIRED_ARGS(windows): -L/INCREMENTAL:NO |
| 5 | +*/ |
| 6 | +// Incremental linking must be turned off, or it will add padding. |
| 7 | +module sectiondefs; |
| 8 | +import core.attribute; |
| 9 | + |
| 10 | +version (linux) |
| 11 | + version = ELF; |
| 12 | +else version (FreeBSD) |
| 13 | + version = ELF; |
| 14 | +else version (OpenBSD) |
| 15 | + version = ELF; |
| 16 | +else version (NetBSD) |
| 17 | + version = ELF; |
| 18 | + |
| 19 | +version(Windows) |
| 20 | + enum PlatformEntryName(string Name) = "." ~ Name ~ "$N"; |
| 21 | +else |
| 22 | + enum PlatformEntryName(string Name) = Name; |
| 23 | + |
| 24 | +mixin template SectionRange(string SectionName, Type) |
| 25 | +{ |
| 26 | + version (OSX) |
| 27 | + { |
| 28 | + enum Segment = (is(Type == const) || is(Type == immutable)) ? "__TEXT" : "__DATA"; |
| 29 | + |
| 30 | + extern(C) extern __gshared |
| 31 | + { |
| 32 | + pragma(mangle, "section$start$" ~ Segment ~ "$" ~ SectionName) |
| 33 | + Type start; |
| 34 | + pragma(mangle, "section$end$" ~ Segment ~ "$" ~ SectionName) |
| 35 | + Type end; |
| 36 | + } |
| 37 | + } |
| 38 | + else version (ELF) |
| 39 | + { |
| 40 | + extern(C) extern __gshared |
| 41 | + { |
| 42 | + pragma(mangle, "__start_" ~ SectionName) |
| 43 | + Type start; |
| 44 | + pragma(mangle, "__stop_" ~ SectionName) |
| 45 | + Type end; |
| 46 | + } |
| 47 | + } |
| 48 | + else version (Windows) |
| 49 | + { |
| 50 | + __gshared |
| 51 | + { |
| 52 | + @section("." ~ SectionName ~ "$A") |
| 53 | + Type _head; |
| 54 | + |
| 55 | + @section("." ~ SectionName ~ "$Z") |
| 56 | + Type _tail; |
| 57 | + } |
| 58 | + |
| 59 | + Type* start() |
| 60 | + { |
| 61 | + return &_head + 1; |
| 62 | + } |
| 63 | + |
| 64 | + Type* end() |
| 65 | + { |
| 66 | + return &_tail; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + Type[] range() |
| 71 | + { |
| 72 | + version (Windows) |
| 73 | + return start()[0 .. end() - start()]; |
| 74 | + else |
| 75 | + return (&start)[0 .. (&end - &start)]; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +mixin SectionRange!("myInts", int) myIntsSection; |
| 80 | +mixin SectionRange!("my8Ints", int[8]) my8IntsSection; |
| 81 | + |
| 82 | +@section(PlatformEntryName!"myInts") |
| 83 | +__gshared int anInt = 2; |
| 84 | + |
| 85 | +@section(PlatformEntryName!"my8Ints") |
| 86 | +__gshared int[8] an8Int = [46, 92, 11, 7, 2, 55, 33, 22]; |
| 87 | + |
| 88 | +void main() |
| 89 | +{ |
| 90 | + //int dummy = anInt, dummy8 = an8Int[0]; |
| 91 | + |
| 92 | + version(none) |
| 93 | + { |
| 94 | + import core.stdc.stdio; |
| 95 | + printf("=========== sectiondefs tests ================\n"); |
| 96 | + printf("myInts %p %zd\n", myIntsSection.range.ptr, myIntsSection.range.length); |
| 97 | + printf("my8IntsSection %p %zd\n", my8IntsSection.range.ptr, my8IntsSection.range.length); |
| 98 | + |
| 99 | + version (Windows) |
| 100 | + printf("start %p %p\n", myIntsSection.start(), my8IntsSection.start()); |
| 101 | + else |
| 102 | + printf("start %p %p\n", &myIntsSection.start, &my8IntsSection.start); |
| 103 | + |
| 104 | + version (Windows) |
| 105 | + printf("end %p %p\n", myIntsSection.end(), my8IntsSection.end()); |
| 106 | + else |
| 107 | + printf("end %p %p\n", &myIntsSection.end, &my8IntsSection.end); |
| 108 | + |
| 109 | + printf("- "); |
| 110 | + foreach (v; myIntsSection.range) |
| 111 | + printf("%d, ", v); |
| 112 | + printf("\n-\n"); |
| 113 | + foreach(v; my8IntsSection.range) |
| 114 | + printf(" - [%d, %d, %d, %d, %d, %d, %d, %d],\n", v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7]); |
| 115 | + printf("\n"); |
| 116 | + } |
| 117 | + |
| 118 | + assert(myIntsSection.range == [2, 9] || myIntsSection.range == [9, 2]); |
| 119 | + |
| 120 | + assert(my8IntsSection.range == [ |
| 121 | + [46, 92, 11, 7, 2, 55, 33, 22], [64, 72, 9, 81, 21, 59, 45, 2] |
| 122 | + ] || my8IntsSection.range == [ |
| 123 | + [64, 72, 9, 81, 21, 59, 45, 2], [46, 92, 11, 7, 2, 55, 33, 22] |
| 124 | + ]); |
| 125 | +} |
0 commit comments