-
-
Notifications
You must be signed in to change notification settings - Fork 700
Implement section attribute #22719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Implement section attribute #22719
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| module extrafiles.sectiondefs; | ||
| import core.attribute; | ||
|
|
||
| version(Windows) | ||
| enum PlatformEntryName(string Name) = "." ~ Name ~ "$N"; | ||
| else | ||
| enum PlatformEntryName(string Name) = Name; | ||
|
|
||
| @section(PlatformEntryName!"myInts") | ||
| __gshared int anInt = 9; | ||
|
|
||
| @section(PlatformEntryName!"my8Ints") | ||
| __gshared int[8] an8Int = [64, 72, 9, 81, 21, 59, 45, 2]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| EXTRA_SOURCES: extra-files/sectiondefs.d | ||
| EXTRA_FILES: extra-files/sectiondefs.d | ||
| REQUIRED_ARGS(windows): -L/INCREMENTAL:NO | ||
| */ | ||
| // Incremental linking must be turned off, or it will add padding. | ||
| module sectiondefs; | ||
| import core.attribute; | ||
|
|
||
| version (ELFv1) | ||
| version = ELF; | ||
| else version (ELFv2) | ||
| version = ELF; | ||
|
|
||
| version(Windows) | ||
| enum PlatformEntryName(string Name) = "." ~ Name ~ "$N"; | ||
| else | ||
| enum PlatformEntryName(string Name) = Name; | ||
|
|
||
| mixin template SectionRange(string SectionName, Type) | ||
| { | ||
| version (OSX) | ||
| { | ||
| enum Segment = (is(Type == const) || is(Type == immutable)) ? "__TEXT" : "__DATA"; | ||
|
|
||
| extern(C) extern __gshared | ||
| { | ||
| pragma(mangle, "section$start$" ~ Segment ~ "$" ~ SectionName) | ||
| Type start; | ||
| pragma(mangle, "section$end$" ~ Segment ~ "$" ~ SectionName) | ||
| Type end; | ||
| } | ||
| } | ||
| else version (ELF) | ||
| { | ||
| extern(C) extern __gshared | ||
| { | ||
| pragma(mangle, "__start_" ~ SectionName) | ||
| Type start; | ||
| pragma(mangle, "__stop_" ~ SectionName) | ||
| Type end; | ||
| } | ||
| } | ||
| else version (Windows) | ||
| { | ||
| __gshared | ||
| { | ||
| @section("." ~ SectionName ~ "$A") | ||
| Type _head; | ||
|
|
||
| @section("." ~ SectionName ~ "$Z") | ||
| Type _tail; | ||
| } | ||
|
|
||
| Type* start() | ||
| { | ||
| return &_head + 1; | ||
| } | ||
|
|
||
| Type* end() | ||
| { | ||
| return &_tail; | ||
| } | ||
| } | ||
|
|
||
| Type[] range() | ||
| { | ||
| version (Windows) | ||
| return start()[0 .. end() - start()]; | ||
| else | ||
| return (&start)[0 .. (&end - &start)]; | ||
| } | ||
| } | ||
|
|
||
| mixin SectionRange!("myInts", int) myIntsSection; | ||
| mixin SectionRange!("my8Ints", int[8]) my8IntsSection; | ||
|
|
||
| @section(PlatformEntryName!"myInts") | ||
| __gshared int anInt = 2; | ||
|
|
||
| @section(PlatformEntryName!"my8Ints") | ||
| __gshared int[8] an8Int = [46, 92, 11, 7, 2, 55, 33, 22]; | ||
|
|
||
| void main() | ||
| { | ||
| //int dummy = anInt, dummy8 = an8Int[0]; | ||
|
|
||
| version(none) | ||
| { | ||
| import core.stdc.stdio; | ||
| printf("=========== sectiondefs tests ================\n"); | ||
| printf("myInts %p %zd\n", myIntsSection.range.ptr, myIntsSection.range.length); | ||
| printf("my8IntsSection %p %zd\n", my8IntsSection.range.ptr, my8IntsSection.range.length); | ||
|
|
||
| version (Windows) | ||
| printf("start %p %p\n", myIntsSection.start(), my8IntsSection.start()); | ||
| else | ||
| printf("start %p %p\n", &myIntsSection.start, &my8IntsSection.start); | ||
|
|
||
| version (Windows) | ||
| printf("end %p %p\n", myIntsSection.end(), my8IntsSection.end()); | ||
| else | ||
| printf("end %p %p\n", &myIntsSection.end, &my8IntsSection.end); | ||
|
|
||
| printf("- "); | ||
| foreach (v; myIntsSection.range) | ||
| printf("%d, ", v); | ||
| printf("\n-\n"); | ||
| foreach(v; my8IntsSection.range) | ||
| 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]); | ||
| printf("\n"); | ||
| } | ||
|
|
||
| assert(myIntsSection.range == [2, 9] || myIntsSection.range == [9, 2]); | ||
|
|
||
| assert(my8IntsSection.range == [ | ||
| [46, 92, 11, 7, 2, 55, 33, 22], [64, 72, 9, 81, 21, 59, 45, 2] | ||
| ] || my8IntsSection.range == [ | ||
| [64, 72, 9, 81, 21, 59, 45, 2], [46, 92, 11, 7, 2, 55, 33, 22] | ||
| ]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,6 +83,32 @@ else | |
| // GDC and LDC declare this attribute in their own modules. | ||
| } | ||
|
|
||
| /** | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. documentation starts here |
||
| * When applied to a global variable, causes it to be emitted to a | ||
| * non-standard object file/executable section. | ||
| * | ||
| * The target platform might impose certain restrictions on the format for | ||
| * section names. | ||
| * | ||
| * Examples: | ||
| * --- | ||
| * import core.attributes; | ||
| * | ||
| * @section("mySection") int myGlobal; | ||
| * --- | ||
| */ | ||
| version (DigitalMars) | ||
| { | ||
| struct section | ||
| { | ||
| string name; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| // GDC and LDC declare this attribute in their own modules. | ||
| } | ||
|
|
||
| /** | ||
| * Use this attribute to attach an Objective-C selector to a method. | ||
| * | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.