forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Add support for generating uninitialized sections #306
Merged
MichalStrehovsky
merged 1 commit into
dotnet:objwriter/12.x
from
MichalStrehovsky:uninitSection
Nov 8, 2022
Merged
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 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 |
---|---|---|
|
@@ -243,11 +243,16 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName, | |
const char *ComdatName) { | ||
Triple TheTriple(TripleName); | ||
MCSection *Section = nullptr; | ||
SectionKind Kind = (attributes & CustomSectionAttributes_Executable) | ||
? SectionKind::getText() | ||
: (attributes & CustomSectionAttributes_Writeable) | ||
? SectionKind::getData() | ||
: SectionKind::getReadOnly(); | ||
SectionKind Kind; | ||
if (attributes & CustomSectionAttributes_Executable) | ||
Kind = SectionKind::getText(); | ||
else if (attributes & CustomSectionAttributes_Uninitialized) | ||
Kind = SectionKind::getBSS(); | ||
else if (attributes & CustomSectionAttributes_Writeable) | ||
Kind = SectionKind::getData(); | ||
else | ||
Kind = SectionKind::getReadOnly(); | ||
|
||
switch (TheTriple.getObjectFormat()) { | ||
case Triple::MachO: { | ||
unsigned typeAndAttributes = 0; | ||
|
@@ -260,6 +265,9 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName, | |
// boundaries. | ||
typeAndAttributes |= MachO::S_ATTR_PURE_INSTRUCTIONS; | ||
} | ||
if (attributes & CustomSectionAttributes_Uninitialized) { | ||
typeAndAttributes |= MachO::S_ZEROFILL; | ||
} | ||
Section = OutContext->getMachOSection( | ||
(attributes & CustomSectionAttributes_Executable) ? "__TEXT" : "__DATA", | ||
SectionName, typeAndAttributes, Kind); | ||
|
@@ -271,8 +279,11 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName, | |
if (attributes & CustomSectionAttributes_Executable) { | ||
Characteristics |= COFF::IMAGE_SCN_CNT_CODE | COFF::IMAGE_SCN_MEM_EXECUTE; | ||
} else if (attributes & CustomSectionAttributes_Writeable) { | ||
Characteristics |= | ||
COFF::IMAGE_SCN_CNT_INITIALIZED_DATA | COFF::IMAGE_SCN_MEM_WRITE; | ||
Characteristics |= COFF::IMAGE_SCN_MEM_WRITE; | ||
if (attributes & CustomSectionAttributes_Uninitialized) | ||
Characteristics |= COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA; | ||
else | ||
Characteristics |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; | ||
} else { | ||
Characteristics |= COFF::IMAGE_SCN_CNT_INITIALIZED_DATA; | ||
} | ||
|
@@ -299,8 +310,11 @@ MCSection *ObjectWriter::GetSpecificSection(const char *SectionName, | |
} else if (attributes & CustomSectionAttributes_Writeable) { | ||
Flags |= ELF::SHF_WRITE; | ||
} | ||
unsigned SectionType = (attributes & CustomSectionAttributes_Uninitialized) | ||
? ELF::SHT_NOBITS | ||
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. for BSS section the SHT_NOBITS should be combined with SHF_ALLOC, to be present in memory, which we get from line 301 above and SHF_WRITE, which we get because we set the writeable attribute in object writer at: case SectionType.Uninitialized:
attributes |= CustomSectionAttributes.Uninitialized | CustomSectionAttributes.Writeable;
break; Looks right to me. |
||
: ELF::SHT_PROGBITS; | ||
Section = | ||
OutContext->getELFSection(SectionName, ELF::SHT_PROGBITS, Flags, 0, | ||
OutContext->getELFSection(SectionName, SectionType, Flags, 0, | ||
ComdatName != nullptr ? ComdatName : ""); | ||
break; | ||
} | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is zerofill needed for correctness or just MachO wants that for uninitialized data?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without that we were generating a lot of zeros in the object file I generated.
It does the same thing as
COFF::IMAGE_SCN_CNT_UNINITIALIZED_DATA
for PE andELF::SHT_NOBITS
for ELF below. But yeah, I don't know why LLVM has both these format specific enums andSectionKind
above.I haven't actually tried end-to-end on mac, but the object file looks about right now in llvm-objdump.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. I am not familiar with MachO at this level. I guess it needs to fill the space with zeroes one way or another. So it is either zeroes in the obj file or a fill it with zeroes at load/demand time. The flag could be selecting that.
Makes sense.